简体   繁体   中英

how to call this function in swift?

I have this function but the problem is I don't know how to call this function on super.viewDidLoad

  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        if let text = namaTextField.text {
            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count < 3 || !text.contains("@")) {
                    floatingLabelTextField.errorMessage = "Invalid email"
                }
                else {
                    // The error message will only disappear when we reset it to nil or empty string
                    floatingLabelTextField.errorMessage = ""
                }
            }
        }
        return true
    }

It is textField Delegate method, Every time when you try to change text in TextField this method call before changes so you can choose weather to do change or not.

And as per my experience you can't call it at your own.

That looks like a delegate method from UITextFieldDelegate . Your view controller class should be conforming to UITextFieldDelegate .

Text field delegate methods are not supposed to be called by the VC. They are supposed to be called by the text field.

This particular method is asking you "should I change the this range of text to this replacement string ?" and in the method you did some logic and returned true (you answers "yes").

This method will be called whenever the current text of the text field changes to ask you "should this part of the text really change?".

The text field will call this method so don't worry about it. The only thing you should do is to set self as the delegate :

namaTextField.delegate = self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM