Reputation: 176
I have four text fields for address, city, state, and zip. I need to call a Picker View for the state field only, so I need to hide the keyboard only on the state text field. I'm trying to do this in the textFieldShouldBeginEditing function using this piece of code, but it's applying it to all text fields.
func textFieldShouldBeginEditing(state: UITextField) -> Bool {
return false
}
I am also applying this piece of code to all text fields so the keyboard will hide on return
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.view.endEditing(true)
return false
}
So all of my text fields are here in the viewDidLoad
address.delegate = self
city.delegate = self
zip.delegate = self
state.delegate = self
I hope this is specific enough. Thanks for your help!
Upvotes: 2
Views: 9284
Reputation: 15804
in the delegate call-back, you can verify the 'textField' with your text-fields. Ex:
func textFieldShouldReturn(textField: UITextField!) -> Bool {
if textField == address {
self.view.endEditing(true)
return false
}
...
}
Upvotes: 3
Reputation: 19922
First of all, by doing this
func textFieldShouldBeginEditing(state: UITextField) -> Bool {
return false
}
you're not "selecting" the textField, you are just naming that variable.
What you should do is to compare that variable to your properties and act depending on that.
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if(textField == self.state)
return false
return true;
}
PS. address
, city
, etc. are not very good variable names. Sure, you know what they are, but if somebody else is looking at the code it won't be obvious, I would recommend using names like addressTextField
or even addressField
instead.
Upvotes: 3