简体   繁体   中英

How to get the text of a textfield after every tap on a keyboard (Swift iOS)

I have a textfield which opens up a keyboard, but I need to somehow get the letter that the user types after every tap of the keyboard, like a live update.

I think the best way of doing this is using some kind of function to constantly check the text of the textfield, but I'm not sure.

Any help is appreciated. Thanks!

First set delegate of the textfield, then use the following code

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

let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
}

You could use a target on your textField with the control event EditingChanged instead of the delegate method. myTextField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged) Then use the targeted method to run your checks.

  func didChangeText(textField:UITextField)
 { 
if textField.text == "apple" 
{ 
 checkImageView1.hidden = false 
}
 else 
  { 
    checkImageView1.hidden = true 
  }
 }

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