如何检查UITextField何时更改? [英] How do I check when a UITextField changes?

查看:152
本文介绍了如何检查UITextField何时更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查文本字段何时更改,相当于textView使用的函数 - textViewDidChange 到目前为止我已经这样做了:

I am trying to check when a text field changes, equivalent too the function used for textView - textViewDidChange so far I have done this:

  func textFieldDidBeginEditing(textField: UITextField) {
        if self.status.text == "" && self.username.text == "" {
            self.topRightButton.enabled = false
        } else {   
            self.topRightButton.enabled = true
        }
    }

哪种工作,但 topRightButton 是在按下文本字段后立即启用,我希望仅在实际输入文本时才启用它?

Which kind of works, but the topRightButton is enabled as soon as the text field is pressed on, I want it to be enabled only when text is actually typed in?

推荐答案

SWIFT

textField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

然后你可以调用你的函数!

Then you can just call your function!

func textFieldDidChange(textField: UITextField) {
    //your code
}

SWIFT 2.2

textField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)

func textFieldDidChange(textField: UITextField) {
    //your code
}

SWIFT 3

textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)

func textFieldDidChange(_ textField: UITextField) {

}

Swift 4

@objc func textFieldDidChange(_ textField: UITextField) {

}

OBJECTIVE-C

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

和textFieldDidChange方法是

and textFieldDidChange method is

-(void)textFieldDidChange :(UITextField *) textField{
    //your code
}

这篇关于如何检查UITextField何时更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆