ResignFirstResponder不会关闭键盘(iPhone)

时间:2010-07-27 17:05:04

标签: iphone keyboard uitextfield uialertview dismiss

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

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

    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,并且在用户点击Done时实现textFieldShouldReturn:试图解除键盘。

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

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

6 个答案:

答案 0 :(得分:6)

首先,您需要在接口标头中定义enterNameAlert。然后使用以下代码。

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

答案 1 :(得分:0)

如果在调用resignFirstResponder方法后将文本字段的释放移动到委托方法会发生什么?

答案 2 :(得分:0)

尝试在TextField的“DidEndOnExit”事件中使用resignFirstResponder关闭键盘。

然后在完成将数据输入TextField后,按键盘上的Done按钮尝试关闭键盘。

希望这有帮助。

答案 3 :(得分:0)

让另一个物体成为然后立即辞职第一响应者。

答案 4 :(得分:0)

简单地做

[mytextField addTarget:self 
            action:@selector(methodToFire:)
            forControlEvents:UIControlEventEditingDidEndOnExit];

答案 5 :(得分:0)

如果有任何人在故事板中遇到这个问题,我必须进入我的uitextfields属性并从其委托中拖放到其根视图控制器全部在故事板中并修复了所有内容..