How to get UITextField text editing changed events for user input validation - Swift Tips 1

Опубликовано: 24 Июль 2015
на канале: Paul Solt
26,657
115

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...


Смотрите видео How to get UITextField text editing changed events for user input validation - Swift Tips 1 онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Paul Solt 24 Июль 2015, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 26,657 раз и оно понравилось 115 людям.