ResignFirstResponder does not cancel the keyboard (iPhone)

I looked at this site that I could not find a solution to the problem that I am currently facing. Hope someone can help.

I created UIAlertView to prompt the user to enter their name in the iPhone app.

UIAlertView *enterNameAlert = [[UIAlertView alloc] initWithTitle:@"Enter your name" message:@"\n\n\n" delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"OK", nil),nil]; UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)]; enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert; enterNameField.borderStyle = UITextBorderStyleRoundedRect; enterNameField.autocorrectionType = UITextAutocorrectionTypeNo; enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing; enterNameField.returnKeyType = UIReturnKeyDone; enterNameField.delegate = self; [enterNameField becomeFirstResponder]; [enterNameAlert addSubview:enterNameField]; [enterNameAlert show]; [enterNameAlert release]; [enterNameField release]; 

I set this viewController to match UITextFieldDelegate in the header file and textFieldShouldReturn: to remove the keyboard when the user clicked Finish.

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; NSLog(@"text field was first responder"); } else { [textField becomeFirstResponder]; [textField resignFirstResponder]; NSLog(@"text field was not first responder"); } NSLog(@"textfieldshouldreturn"); return YES; } 

In the debugger, I confirm that this method is called successfully when I click the Finish button, with textField being the first responder at that time. However, the keyboard does not disappear, but the small clearButton leaves. It is clear that textField is no longer the first responder, because when I click the Finish button again, nothing is called. I just want to remove the keyboard using the "Finish" button. Can anyone suggest a solution? Thanks a million.

+6
iphone uitextfield keyboard uialertview dismiss
source share
6 answers

First you need to define your enterNameAlert in the interface header. then use the code below.

 - (void)viewDidLoad { [super viewDidLoad]; enterNameAlert = [[UIAlertView alloc] initWithTitle:@"Enter your name" message:@"\n\n\n" delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"OK", nil),nil]; UITextField *enterNameField = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)]; enterNameField.keyboardAppearance = UIKeyboardAppearanceAlert; enterNameField.borderStyle = UITextBorderStyleRoundedRect; enterNameField.autocorrectionType = UITextAutocorrectionTypeNo; enterNameField.clearButtonMode = UITextFieldViewModeWhileEditing; enterNameField.returnKeyType = UIReturnKeyDone; enterNameField.delegate = self; [enterNameAlert addSubview:enterNameField]; [enterNameField becomeFirstResponder]; [enterNameAlert show]; [enterNameField release]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { if ([textField isFirstResponder]) { [textField resignFirstResponder]; [enterNameAlert dismissWithClickedButtonIndex:1 animated:YES]; [enterNameAlert release]; NSLog(@"text field was first responder"); } else { [textField becomeFirstResponder]; [textField resignFirstResponder]; NSLog(@"text field was not first responder"); } NSLog(@"textfieldshouldreturn"); return YES; } 
+6
source share

What happens if you move the selection of the text field to the delegate method after calling the resignFirstResponder method?

0
source share

Try to cancel the keyboard using resignFirstResponder in the "DidEndOnExit" event of your TextField.

Then try to remove the keyboard by clicking the Finish button on the keyboard when you are finished entering data into your TextField.

Hope this helps.

0
source share

There is another object, and then immediately reconcile with the first responder.

0
source share

just follow

 [mytextField addTarget:self action:@selector(methodToFire:) forControlEvents:UIControlEventEditingDidEndOnExit]; 
0
source share

If someone had problems with this in the storyboard, I had to go into my uitextfields properties and ctrl + drag everything from the delegate to my root view controller in the storyboard, and that fixed everything.

0
source share

All Articles