The apple docs offer:
Asks the delegate if the text field should process the pressing of the
return button.- (BOOL)textFieldShouldReturn:(UITextField *)textFieldParameters
textFieldThe text field whose return button was pressed.
Return ValueYESif the text field should implement its default
behavior for the return button; otherwise,NO.Discussion The text field callsthis method whenever the user tapsthe
return button. You can use this method to implement any custom
behavior when the button is tapped.
My question is what does the return value do? I have been implementing the behavior in this method so it makes no difference what is returned. Is this not the correct method to perform the action?
For instance, if I implement a search function, should I trigger the search action in this method or somewhere else.
This is the correct method to trigger an action when the user taps the Return keyboard key (whatever it happens to be labeled).
The return value from the
textFieldShouldReturn:delegate method almost never matters. If you are dealing with a single text field then it definitely doesn’t matter.I ran into one issue a while back that made me realize that just under the right situation, the return value does matter. I had a screen with several text fields and then a text view. I was using this text field delegate method to change the first responder from text field to text field to text view. I found that if I returned
YESin this delegate method and then made the text view the first responder, the newline was being sent to the text view.As a result of this, I now always return
NOfrom this delegate method to be safe.