ResignFirstResponder doesn't dismiss the keybo

2019-01-23 19:33发布

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.

6条回答
Lonely孤独者°
2楼-- · 2019-01-23 20:02

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;
}
查看更多
Animai°情兽
3楼-- · 2019-01-23 20:02

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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-23 20:08

simply do

[mytextField addTarget:self 
            action:@selector(methodToFire:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-23 20:25

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..

查看更多
时光不老,我们不散
6楼-- · 2019-01-23 20:28

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

查看更多
太酷不给撩
7楼-- · 2019-01-23 20:29

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

查看更多
登录 后发表回答