Stop 'Done' button on keyboard from putting keyboard away
When i press the done button on the keyboard, it automatically puts the 开发者_开发问答keyboard away. How do i stop it from doing this?
Make sure that your view controller is set as the UITextField
's delegate, then implement the shouldEndEditing: method. Just return NO
to keep the text field active and keep the keyboard on the screen.
See Apple's docs here.
Of course, this may not be the best approach but after reading the docs above you should be able to find the right callback and control your program's flow appropriately.
Your controller needs to implement the textFieldShouldReturn:
method of the UITextFieldDelegate
(reference):
Make sure that your view controller has been set as the delegate for your textfield, either through Interface Builder or in code:
- (void) viewDidLoad
{
self.someTextField.delegate = self;
}
Then, in your view controller, you will need to implement the textFieldShouldReturn:
method:
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
return NO;
}
What you are specifying is that no action should occur when the return (done) key is pressed on the keyboard. What is odd, as others have mentioned, is that this is precisely the default behavior. This means that 1) you are calling the resignFirstResponder
on your text field or, 2) this text field is inside a UIWebView
and good luck changing that behavior.
I had the similar situation. When [myTextField resignResponder] occurred, the keyboard disappeared, but I just wanted to keep the keyboard appearing in the screen.
I just made one extra UITextField instance outside the screen, say dummyTextField, and its frame is CGRectMake(-10,-10,5,5).
And after [myTextField resignResponder], I immediately called [dummyTextField becomeFirstResponder]. So the keyboard is still alive, while the user can't see dummyTextField.
I believe there's a better way to do it. But I did in this way, and it worked fine.
精彩评论