0

I have cerated a custom UITextfield class according to my application UI. But I want some of the delegate methods to be included into the class so I do not need to write them in all the classes.

open class SAIconTextField: SkyFloatingLabelTextFieldWithIcon {

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    configure()
}

/// Override init method
override init(frame: CGRect) {
    super.init(frame: frame)
    configure()
}

/// Override layoutSubviews method
override open func layoutSubviews() {
    super.layoutSubviews()
}

///  This method is used to set general UI
func configure() {
    self.tintColor = UIColor.appBlue
    self.selectedTitleColor = .appBlue
    self.selectedLineColor = .appBlue
    self.selectedLineHeight = 0.5

    self.iconColor = .lightGray
    self.iconImageView.tintColor = .lightGray
    self.selectedIconColor = .appBlue
    self.iconMarginBottom = 7.0
    //        self.iconMarginLeft = 2.0
    self.errorColor = .errorColor
}
}

I have extended my class to perform a delegate method so that all the textfields prevent entering emojis in my app, but the method is not called.

// MARK: - UITextFieldDelegate
extension SAIconTextField: UITextFieldDelegate {

/// Delegate method
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.textInputMode?.primaryLanguage == "emoji" || !((textField.textInputMode?.primaryLanguage) != nil) {
        return false
    }
    return true
}
}

Any other way to do the similar things?

Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30

2 Answers2

2

You don't set up your text field delegate.

Add this line to your configure() function:

delegate = self
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • adding delegate = self in class; calls class delegate methods but avoids calling view controller's delegate methods that are not similar for all the classes. So either class delegate method can be called or ViewController's. Is there any escape window so I can call delegate methods from both of them? – Krutika Sonawala Nov 12 '18 at 08:22
  • As Stas Baranovskiy wrote, you can’t set two diffrent delegate objects at the same time – Robert Dresler Nov 12 '18 at 08:27
1

According the code you've shared, I could say that UITextFieldDelegate's function textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool is not called, since delegate object is not set.

Stanislau Baranouski
  • 1,425
  • 1
  • 18
  • 22
  • 1
    Also you can't set two different delegate objects at the same time. I.e. to set you custom textfield class handle only one delegate function and your viewcontroller to handle the rest. To achieve this you have to "proxy" delegate callbacks. I also recommend you to read following topic - https://stackoverflow.com/questions/32542362/swift-2-0-uitextfielddelegate-protocol-extension-not-working – Stanislau Baranouski Nov 12 '18 at 08:21