Reputation: 817
I have an iPad application in which resignFirstResponder does't seems to be working. I have tried many solutions. I am just calling resign first responder from the instance of the firstResponder object but keyboard is remain there on the screen. THen I tried by iterating all the window to get the instance of the first responder and then I am calling the resignFirstResponder from that instance.
I also try by creating the category of the UIViewController
for the following methods.
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
But this solution is also not working for me. And this keyboard problem is there in all the textfield of application not only for some specific textfield.
Update: In different part of the app I am using different code for this purpose. Here is the code
- (void)textFieldDidEndEditing:(UITextField *)mtextField
{
[mtextField resignFirstResponder]; this one is not required but I have just write it here.
}
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string hasSuffix:@"\n"])
{
[theTextField resignFirstResponder];
return NO;
}
return YES;
}
Update: When I try by running the application in the simulator, some times it resignFirstResponder
is working, but not on every launch even in simulator.
Upvotes: 0
Views: 3313
Reputation: 14841
First of all, you don't call methods in Objective-C, rather you send messages to objects.
UIView
isn't the one who should send the message resignFirstResponder
. Have your UITextField
as a property - let's call it myTextField. You should use this:
[self.myTextField resignFirstResponder];
Upvotes: 0
Reputation: 817
I got the solution for this problem. Actually there seems to some change in the apple cocoa API. In my app I have an UIAlertView
, which we used to display to show that some operation is going on and user should wait to finish it. For that purpose the code I use in my app was
- (void)displayAlertView
{
UIAlertView *alertView = creating the object;
UIActivityIndicatorView *activityIndicator= creating the object;
[alertView addSubview:activityIndicator];
[activityIndicator startAnimating];
[alertView show];
[alertView release];
}
-(void)didPresentAlertView:(UIAlertView *)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
The problem get resolved when I use the code below in my application
-(void)didPresentAlertView:(UIAlertView *)alertView
{
[self performSelector:@selector(dismissView:) withObject:alertView afterDelay:0.0];
}
-(void)dismissView:(UIAlertView*)alertView
{
[alertView dismissWithClickedButtonIndex:0 animated:NO];
}
I think here some thing is going wrong in the UIAlertView
, and because of that keyboard is not hiding when resigning as the first responder.
Upvotes: 0
Reputation:
If you want the keyboard to resign when you press the return key, you need to use:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Then the textFieldDidEndEditing gets invoked.
Upvotes: 1
Reputation: 3143
the simplest and best way to hide the keyboard is to use
[self.view endEditing:YES];
and make sure you are setting appropriate delegate of the textfields
Upvotes: 5