如何检查UITextField何时更改?

我正在尝试检查一个文本字段的变化,相当于用于textView的function – textViewDidChange到目前为止,我已经这样做了:

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

哪种types的作品,但topRightButton文本字段被按下, topRightButton被启用了,我希望只有当文本被input时才能使用它。

迅速

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

那么你可以打电话给你的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) { } 

Objective-C的

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

和textFieldDidChange方法是

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

您可以在界面构build器中进行此连接。

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

  2. Ctrl +点击界面生成器中的文本框。

  3. 从EditingChanged拖到助理视图中的视图控制器类中。 建立联系

  4. 命名您的function(例如“textDidChange”),然后单击连接。 命名功能

Swift 3.0

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

和处理方法:

 func textFieldDidChange(textField: UITextField) { } 

Swift 4.0

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

和处理方法:

 @objc func textFieldDidChange(_ textField: UITextField) { } 

Swift 3

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

我到目前为止处理它的方式:在UITextViewDelegate

 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 } 

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

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

你可以从UITextFieldDelegate中使用这个委托方法。 它随着每个angular色的变化而发生。

 (Objective C) textField:shouldChangeCharactersInRange:replacementString: (Swift) textField(_:shouldChangeCharactersInRange:replacementString:) 

然而,这只是在改变之前进行的(事实上,只有当你从这里返回时,才会有改变)。

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 } 

也许使用RxSwift?

需要

 pod 'RxSwift', '~> 3.0' pod 'RxCocoa', '~> 3.0' 

明显增加import

 import RxSwift import RxCocoa 

所以你有一个textfield : UITextField

 let observable: Observable<String?> = textField.rx.text.asObservable() observable.subscribe( onNext: {(string: String?) in print(string!) }) 

你还有其他3种方法

  1. onerror的
  2. onCompleted
  3. onDisposed
  4. onNext

这是你如何使用Swift 3添加一个textField text change listener器:

将您的类声明为UITextFieldDelegate

 override func viewDidLoad() { super.viewDidLoad() textField.delegate = self textField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:)), for: UIControlEvents.editingChanged) } 

然后传统上添加一个textFieldShouldEndEditing函数:

 func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { // do stuff return true }