views:

18693

answers:

8

I know that I need to tell my UITextField to resign first responder when I want to dismis the keyboard, but I'm not sure how to know when the user has pressed the "Done" key on the keyboard. Is there a notification I can watch for?

+9  A: 

If you connect the text field to an action (IBAction) in InterfaceBuilder, it will be messaged when the user dismisses the keyboard and the sender will be a reference to the UITextField that fired the event.

For example:

-(IBAction)userDoneEnteringText:(id)sender
{
    UITextField theField = (UITextField*)sender;
    // do whatever you want with this text field
}

Then, in InterfaceBuilder, link the text field to this action on your controller (or whatever you're using to link events from the UI). Whenever the user enters text and dismisses the text field, the controller will be sent this message.

Jason Coco
The only event that seems like it might work is textFieldDidEndEditing, which states it's only sent after the UITextField resigns first responder status. How do I know when I need to resign first responder status?
kubi
I'm sorry about the terminology... it's not an actual notification (event)... I'm going to edit my answer with a quick example and explanation, so see that :)
Jason Coco
To clarify this answer, the textField sends the "Did End On Exit" event when you hit the return key, so that's the one you need to link to your controller. I think this method is easier than the one I outlined above.
kubi
+40  A: 

I set the delegate of the UITextField to my ViewController class. In that class I implimented this method

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
kubi
See the example above... wire that using IntefaceBuilder to the action on whatever you're using as your controller... whenever the user dismisses the text entry system (presses done), this will be invoked...
Jason Coco
Either of these will work - the text field sends its action when the user hits the Return ("Done", whatever) key, and also sends its delegate -textFieldShouldReturn:.
Noah Witherspoon
BTW - this also works for a UISearchBar. For example, my code I had a member IBOutlet searchBarTusb; When my 'return key' (UIReturnKeySearch) was clicked, -(void) searchBarSearchButtonClicked:, I put the statement [searchBarTusb resignFirstResponder];Also note, the 'return key' was set in the NIB using Interface Builder.
mobibob
+3  A: 

kubi, thanks. Your code worked. Just to be explicit (for newbies like) as you say you have to set the UITextField's delegate to be equal to the ViewController in which the text field resides. You can do this whereever you please. I chose the viewDidLoad method. Thnx

  - (void)viewDidLoad 
    {
        /*sets the textField delegates to equal this viewController ... this allows for the keyboard to disappear after pressing done*/
        daTextField.delegate = self;
    }
+1  A: 

Chris, What happens if there is more than one text field ? Also how do you know WHEN the Done or Return key is pressed ?

textFieldShouldReturn gets called when the Return key is pressed (or Done, or Next, whatever you chose it to be called). If there is more than one text field just inspect which one is passed to the textFieldShouldReturn call.
Dan J
+5  A: 

You can also create a method in your controller

 -(IBAction)editingEnded:(id)sender{
    [sender resignFirstResponder]; 
}

and then in Connection Inspector in IB connect Event "Did End On Exit" to it.

Hugo
+1  A: 

You will notice that the method "textFieldShouldReturn" provides the text-field object that has hit the DONE key. If you set the TAG you can switch on that text field. Or you can track and compare the object's pointer with some member value stored by its creator.

My approach is like this for a self-study:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(@"%s", __FUNCTION__);

bool fDidResign = [textField resignFirstResponder];

NSLog(@"%s: did %resign the keyboard", __FUNCTION__, fDidResign ? @"" : @"not ");

return fDidResign;

}

Meanwhile, I put the "validation" test that denies the resignation follows. It is only for illustration, so if the user types NO! into the field, it will not dismiss. The behavior was as I wanted, but the sequence of output was not as I expected.

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    NSLog(@"%s", __FUNCTION__);

    if( [[textField text] isEqualToString:@"NO!"] ) {
     NSLog(@"%@", textField.text);
     return NO;
    } else {
     return YES;
    }
}

Following is my NSLog output for this denial followed by the acceptance. You will notice that I am returning the result of the resign, but I expected it to return FALSE to me to report back to the caller?! Other than that, it has the necessary behavior.

13.313 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]
13.320 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:]
13.327 StudyKbd[109:207] NO!
13.333 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard
59.891 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]
59.897 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldEndEditing:]
59.917 StudyKbd[109:207] -[StudyKbdViewController doneEditText]: NO
59.928 StudyKbd[109:207] -[StudyKbdViewController textFieldShouldReturn:]: did resign the keyboard
mobibob
A: 

how to dismiiss keyboard,frmo textview?

"TEXTVIEW"

regards shishir

shishir.bobby
Hi Shishir. I don't understand what your question is and you should either make a new question for this or put it in a comment for someone else. Questions aren't appropriate as answers to other questions.
kubi
A: 

just add [textField endEditing:YES];

where you want to disable keyboard and display pickerview