How to get UITextField text editing changed events for user input validation - Swift Tips 1
Watch the new Swift 4 version: • How to Get UITextField Text Editing C...
Get the code: http://iPhoneDev.tv/SwiftTips
Learn how to get user input from text fields in your iPhone App. When a user types in a UITextField, you can get a method call for each character typed. This is important when you want to validate input or warn the user about invalid input.
You can provide real-time feedback to the user with the Text Editing Changed Events.
Right-click on a UITextField, drag a connection for the Editing Changed Event (sent events) or connect it in code using the UIControlEvents.EditingChanged enum value.
@IBAction func textFieldEditingDidChange(_ sender: AnyObject) {
print("textField: \(textField.text!)")
if validatePassword(text: textField.text!) {
// correct password
button.isEnabled = true
} else {
button.isEnabled = false
}
}
Code to add Events to UITextField
You can optionally do the connection in code, instead of using Assistant Editor. In viewDidLoad: add the code for any UITextField.
override func viewDidLoad() {
super.viewDidLoad()
// disable button
button.isEnabled = false
// The action can be made in code, uncomment to try it
// textField.addTarget(self, action: Selector("textFieldEditingDidChange:"), for: UIControlEvents.editingChanged) // Swift 2
// Swift 4 (you can leave off the parameters)
// textField.addTarget(self, action: #selector(textFieldEditingDidChange), for: UIControlEvents.editingChanged)
// Swift 4 (you can specify the parameters) use (_:) since the label is now _ in Swift 4
// textField.addTarget(self, action: #selector(textFieldEditingDidChange(_:)), for: UIControlEvents.editingChanged)
}
--
Join the iPhone App Course: http://SuperEasyApps.com
Learn how to make iPhone apps from scratch using Swift 4, Xcode 9, and iOS 11. I teach how to use Storyboard, Auto Layout, UI Design, app ideas, and app usability.
Blog: http://blog.SuperEasyApps.com
Follow Paul: / paulsolt
Email: [email protected]
Community: http://community.supereasyapps.com
Click here to Subscribe: http://www.youtube.com/subscription_c...
Watch video How to get UITextField text editing changed events for user input validation - Swift Tips 1 online without registration, duration hours minute second in high quality. This video was added by user Paul Solt 24 July 2015, don't forget to share it with your friends and acquaintances, it has been viewed on our site 26,657 once and liked it 115 people.