ResignFirstResponder不会关闭键盘(iPhone) [英] ResignFirstResponder doesn't dismiss the keyboard (iPhone)

查看:343
本文介绍了ResignFirstResponder不会关闭键盘(iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个网站上搜索过,我找不到解决我现在面临的问题的方法。希望有人可以提供帮助。

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

我创建了一个UIAlertView来提示用户在iPhone应用程序中输入他们的名字。

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];

我已将此viewController设置为符合头文件中的UITextFieldDelegate,并实现了 textFieldShouldReturn:在用户点击完成时尝试关闭键盘。

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;
}

在调试器中,我确认当我点击时成功调用此方法完成按钮,textField是当时的第一个响应者。然而,键盘并没有消失,但是小小的clearButton消失了。显然textField不再是第一个响应者,因为当我再次单击Done按钮时,不会调用任何内容。我只是想用Done按钮关闭键盘。有人可以提供解决方案吗?百万谢谢。

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.

推荐答案

首先,您需要在界面中定义 enterNameAlert 头。然后使用下面的代码。

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;
}

这篇关于ResignFirstResponder不会关闭键盘(iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆