I have an iOS app, with one UIView
and three UITextField
(more than 1)
I would to understand what are the best practices for my class ViewController to manage the UITextField
.
- class MainViewController: UIViewController, UITextFieldDelegate ?
I wonder that, because I have more than one UITextField
and only one func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
Easiest way is to know what text field to use in delegate methods. I.e. you have 3 text fields: field1, field2, field3 and when delegate called you can detect what to do:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField == field1 {
// do something
} else if textField == field2 {
// do something
} else if textField == field3 {
// do something
}
return true
}
Do not forget to make all field's delegate as self: field1.delegate = self
etc.
In your case it will work fine.
If you want to know a better solution if you have much more fields (10, 20?) let me know and I'll update my answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With