35

This is the text field delegate method but i have doubt about return type

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
  [textField resignFirstResponder];

   return NO;
}  

and this is the same method with different return type

-(BOOL)textFieldShouldReturn:(UITextField *)textField
 {
   [textField resignFirstResponder];

   return YES;
 }

by both we can hide key board in i phone . but what is the meaning of return type "YES" or "NO". I am not seeing any difference.

iKushal
  • 2,689
  • 24
  • 20

4 Answers4

33

In addition to the other answers here (which essentially confirm there's no major difference, no auto-resigning of the first responder by returning YES, etc.), I've discovered a strange occurrence completely dependent on the return value, specifically for a UITextField with autocorrection.

Assume the following scenario:

  1. You have a UITextField with autocorrection enabled

  2. You've implemented the delegate method something like this (where the text field gets resigned):

    - (BOOL)textFieldShouldReturn:(UITextField *)textField 
    {
        if (textField == self.myAutocorrectingTextField) {
            [self.myAutocorrectingTextField resignFirstResponder];
        }
        return YES; 
    }
    
  3. You run the app, type "Cable" into the text field, then press the return key, resigning it as first responder.

The outcome when returning YES:

  • The text field's text will have been changed from "Cable" to "Cables"!

I have tested a handful of other words that respond similarly:

  • "Stuff" changed to "Stuffs"
  • "Ten" to "Tens"
  • And, one time "Car" changed to "Carl", another time "Car" changed to "Carp"

The outcome when returning NO:

Normal. The text field's text will not have been changed.

In Summary...

When returning YES, the text field may autocorrect already-correct words. It does not, however, present one of those "autocorrect text bubbles", because these are legitimate words.

When returning NO, the text field will not autocorrect already-correct words.

Note: In both cases, incorrectly-spelled words will always be corrected. For example:

  • If you were to type "Wev", an "autocorrection text bubble" should appear with the text "Web". If you hit the Return key, regardless of the delegate's return value, the text will be replaced with the correction.
beebcon
  • 6,893
  • 5
  • 25
  • 27
11

One difference that I see based on personal experience is that if you return YES, auto correction and auto capitalization are triggered, if you return NO, they are not. There might be other things done, but that's all I could notice so far.

That's what they mean by "default behavior".

Fabien Warniez
  • 2,731
  • 1
  • 21
  • 30
0

The most important difference I've found is that if the text field emits the control event UIControlEventEditingDidEndOnExit, this will cause the text field to resign first responder unless textFieldShouldReturn is implemented to return NO.

matt
  • 515,959
  • 87
  • 875
  • 1,141
-5

The textFieldShouldReturn is a place where you can handle the Return button on the keyboard.

textFieldShouldReturn asks the delegate if the text field should process the pressing of the return button.

If you implement your own code to process the return button you should return NO, or return YES for it to be handled in the default way.

The IOS Docs state: The text field calls this method whenever the user taps the return button. You can use this method to implement any custom behavior when the button is tapped.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

Craig Mellon
  • 5,399
  • 2
  • 20
  • 25
  • 4
    You've basically just posted the contents of Apple's reference. There's no new information or insight here. – northernman Sep 15 '14 at 03:07
  • I've answered his question. He asked what the difference is, and the difference is if you return YES it will be handled in the default way, if you return NO it wont be handled the default way and you implement custom behavior. How can you down vote me for giving him the information. There isn't much more to it. – Craig Mellon Sep 15 '14 at 11:55
  • 2
    @CraigMellon He's obviously asking what the default behavior is. – Adlai Holler Jul 20 '15 at 22:31
  • northernman and Adlai Holler are right, Craig Mellon has added nothing to this conversation and has not given a good or correct answer here. Fabien Warniez's answer is so much better, so is beebcon's. Can someone change the correct answer to be one of those? – Sam May 09 '20 at 04:18
  • this post was from 2012 and although I do agree beebcon's answer is much better it was added over a year after. Have people not got anything better to do than go through all the old questions and nit pick about them. I've been using and posting on stack overflow for nearly 10 years, have 5 times more rep than you and you comment stuff like that. Why not spend your time better by answering some questions??? – Craig Mellon Jul 02 '20 at 11:20