ResignFirstResponder doesn't dismiss the keyboard (iPhone)
Asked Answered
A

6

7

I've searched through this site that I couldn't find a solution to the problem I'm facing now. Hope someone can help.

I've created a UIAlertView to prompt user to enter their name in an 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've setup this viewController to comply with UITextFieldDelegate in the header file, and implemented textFieldShouldReturn: trying to dismiss the keyboard when user hit Done.

- (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 tap on the Done button, with the textField being the first responder at that time. However, the keyboard doesn't go away, but the little clearButton goes away. It is clear that the textField is no longer the first responder because when I click the Done button again, nothing get called. I just want to dismiss the keyboard with the Done button. Can anyone please offer a solution? Million thanks.

Affinity answered 27/7, 2010 at 17:5 Comment(1)
What happens if you set the text field as a property in your view controller, thereby ensuring that you've got it retained even after the alert has been dismissed? I'm wondering if perhaps it's getting deallocated before the event loop gets around to sending the resignFirstResponder message or something weird like that. P.S. Did we used to work together?Warlord
S
6

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

- (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;
}
Soot answered 24/11, 2010 at 11:29 Comment(2)
This works!! But I have to remove [enterNameAlert release] from the -(BOOL)textFieldShouldReturn:(UITextField *)textField method. Otherwise, the app will simply crash and quit after I click Done button. But I'm not really familiar with the memory stuff. Will there be any memory leak by doing so?Affinity
If you don't want to release enterNameAlert within textFieldShouldReturn. Its OKay. But you will have to release it elsewhere inside the implementation. lets say: -dealloc{ [enterNameAlert release]; [super dealloc]; }Soot
S
0

What happens if you move the releasing of the text field to the delegate method, after you call the resignFirstResponder method?

Shipwright answered 7/10, 2010 at 20:49 Comment(0)
E
0

Try dismissing keyboard using resignFirstResponder on "DidEndOnExit" event of your TextField.

Then try to dismiss keyboard by pressing Done button on keyboard when you have finished entering the data into your TextField.

Hope this helps.

Earl answered 12/10, 2010 at 4:27 Comment(2)
Implement resignFirstResponder on -(IBAction)DidEndOnExit : (id)sender { //Your Code Goes Here }Earl
Did this above solution help?Earl
S
0

Have another object become and then immediately resign the first responder.

Submariner answered 14/10, 2010 at 14:34 Comment(1)
Sorry, I'm not so sure how can I achieve this.Affinity
H
0

simply do

[mytextField addTarget:self 
            action:@selector(methodToFire:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
Hydrosome answered 26/1, 2012 at 21:52 Comment(0)
E
0

if anyone is having trouble with this in storyboard i had to go into my uitextfields properties and ctrl+drag from its delegate to its root view controller all in storyboard and that fixed everything..

Eb answered 11/4, 2013 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.