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.

Anthony
  • 747
  • 1
  • 8
  • 23
  • 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? – clozach Aug 30 '10 at 10:12

6 Answers6

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;
}
mxcl
  • 26,392
  • 12
  • 99
  • 98
Usama Ahmed
  • 158
  • 1
  • 9
  • 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? – Anthony Dec 03 '10 at 10:39
  • 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]; } – Usama Ahmed Dec 14 '10 at 06:45
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..

rezand
  • 576
  • 3
  • 11
0

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

Ben
  • 2,982
  • 1
  • 16
  • 12
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.

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
0

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

Mike A
  • 2,501
  • 2
  • 23
  • 30
0

simply do

[mytextField addTarget:self 
            action:@selector(methodToFire:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
Flexo
  • 87,323
  • 22
  • 191
  • 272
rajomato
  • 1,167
  • 2
  • 10
  • 25