如何检查UITextField何时更改?

265 uitextfield ios swift

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

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

哪种工作,但topRightButton只要按下文本字段就启用,我希望只有在实际输入文本时才能启用它?

Faw*_*sud 674

迅速

textfield.addTarget(self, action: #selector(ViewControllerr.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
Run Code Online (Sandbox Code Playgroud)

然后你可以打电话给你的功能!

@objc func textFieldDidChange(_ textField: UITextField) {

}
Run Code Online (Sandbox Code Playgroud)

SWIFT 2.2

textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Run Code Online (Sandbox Code Playgroud)

func textFieldDidChange(_ textField: UITextField) {

}
Run Code Online (Sandbox Code Playgroud)

SWIFT 3&swift 4.1

textField.addTarget(self, action: #selector(YourViewController.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
Run Code Online (Sandbox Code Playgroud)

func textFieldDidChange(textField: UITextField) {
    //your code
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

[textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
Run Code Online (Sandbox Code Playgroud)

Objective-C的

-(void)textFieldDidChange :(UITextField *) textField{
    //your code
}
Run Code Online (Sandbox Code Playgroud)

和textFieldDidChange方法是

textfield.addTarget(self, action: #selector(ViewControllerr.textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
Run Code Online (Sandbox Code Playgroud)


rmo*_*ney 117

您可以在界面构建器中建立此连接.

  1. 在故事板中,单击屏幕顶部的助理编辑器(中间的两个圆圈). 助理编辑选中

  2. Ctrl +单击界面构建器中的文本字段.

  3. 从EditingChanged拖动到助手视图中的视图控制器类内部. 建立联系

  4. 为您的函数命名(例如"textDidChange"),然后单击"连接". 命名功能

  • 这是一个很好的解决方案,尤其是在处理由单独数据源管理的tableViewCell中的UITextField时.这种方法允许viewController直接响应(因此数据源不必响应并委托操作). (3认同)

Rob*_*ert 51

Swift 3.0

textField.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)
Run Code Online (Sandbox Code Playgroud)

和处理方法:

func textFieldDidChange(textField: UITextField) { 

}
Run Code Online (Sandbox Code Playgroud)

Swift 4.0

textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)),
                          for: UIControlEvents.editingChanged)
Run Code Online (Sandbox Code Playgroud)

和处理方法:

@objc func textFieldDidChange(_ textField: UITextField) {

}
Run Code Online (Sandbox Code Playgroud)


Vin*_*zzz 27

到目前为止我处理它的方式:in UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    // text hasn't changed yet, you have to compute the text AFTER the edit yourself
    let updatedString = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)

    // do whatever you need with this updated string (your code)


    // always return true so that changes propagate
    return true
}
Run Code Online (Sandbox Code Playgroud)

Swift4版本

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
    return true
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*zzi 14

斯威夫特3

 textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(sender:)), for: UIControlEvents.editingChanged)
Run Code Online (Sandbox Code Playgroud)


avi*_*ran 7

Swift 3.0.1+(其他一些swift 3.0答案不是最新的)

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

func textFieldDidChange(_ textField: UITextField) {

}
Run Code Online (Sandbox Code Playgroud)


JP *_*ino 7

现在有一个 UITextField 委托方法可用于 iOS13+

optional func textFieldDidChangeSelection(_ textField: UITextField)
Run Code Online (Sandbox Code Playgroud)

  • 此方法涉及选择,而不是实际文本 (2认同)

Abu*_*Dar 6

您可以使用 UITextFieldDelegate 中的此委托方法。它会随着每个角色的变化而触发。

(Objective C) textField:shouldChangeCharactersInRange:replacementString:
(Swift) textField(_:shouldChangeCharactersInRange:replacementString:)
Run Code Online (Sandbox Code Playgroud)

但是,这仅在进行更改之前触发(实际上,只有在您从此处返回 true 时才会进行更改)。


rad*_*ad4 5

textField(_:shouldChangeCharactersIn:replacementString :)在Xcode 8,Swift 3中为我工作,如果你想检查每一个按键.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    // Whatever code you want to run here.
    // Keep in mind that the textfield hasn't yet been updated,
    // so use 'string' instead of 'textField.text' if you want to
    // access the string the textfield will have after a user presses a key

    var statusText = self.status.text
    var usernameText = self.username.text

    switch textField{
    case self.status:
        statusText = string
    case self.username:
        usernameText = string
    default:
        break
    }

    if statusText == "" && usernameText == "" {
        self.topRightButton.enabled = false
    } else {   
        self.topRightButton.enabled = true
    }

    //Return false if you don't want the textfield to be updated
    return true
}
Run Code Online (Sandbox Code Playgroud)


dre*_*ter 5

斯威夫特4

符合UITextFieldDelegate.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // figure out what the new string will be after the pending edit
    let updatedString = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)

    // Do whatever you want here


    // Return true so that the change happens
    return true
}
Run Code Online (Sandbox Code Playgroud)