resignFirstResponder OR - (IBAction) doneButtonOnKeyboardPressed: (id)sender
I was just wondering which approach is better to hide keyboard in iphone application
1> Implement
- (IBAction) doneButtonOnKeyboardPressed: (id)sender
{
}
Method on Textfield 's Did End On Ex开发者_如何学Pythonit Event
OR
In Textfield implement this
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[txtName resignFirstResponder];
return YES;
}
Which Option is better to choose in which situation...? Any one Option has advantage over other...?
I strongly prefer using the target/action pattern by responding to the UIControlEventDidEndOnExit
event, usually by wiring it up in Interface Builder by connecting the "Did End On Exit" event shown in IB to the File's Owner, using the method of my choice. This would be the first option you showed.
Here's why I prefer this mechanism:
It many apps (well, at least my apps), it is necessary to distinguish between canceling input, say, by touching outside the text field, and completion of input by the done button on the keyboard (the docs tend to refer to this as the "return" button). Because the -textFieldDidEndEditing
delegate method gets called any time -resignFirstResponder
is invoked when the field is first responder (regardless of the reason for ending editing), it is necessary to have a variable somewhere to track the path of how you're terminating editing. This introduces a level of complexity that simply isn't necessary.
In my app, I respond to touch events outside the UITextField
, invoking -resignFirstResponder
to cancel editing without taking further action. If I use the delegate methods, I would need to set state here to record that I'm taking the 'cancel' path through my code, and use the -textFieldShouldReturn
delegate method to both set state to indicate I'm going through the 'done' path of my code, and invoke -resignFirstResponder
. Messy.
Using the target/action pattern here leads to simpler, cleaner code.
Functionality is same, one is handling from control "textfield" and other related to keyboard delegate. why do u want to handle keyboard? i suggest handle it from ur controls.....
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[txtName resignFirstResponder];
return YES;
}
Above method is the best - you just need to select your any of textFields & in connections inspector - connect your files owner to delegate of textFields. That will do your things work.
精彩评论