ResignFirstResponder 不会关闭键盘 (iPhone)

时间:2023-01-30
本文介绍了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:@"


"
                                                            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 不再是第一响应者,因为当我再次单击完成"按钮时,什么都没有被调用.我只想用完成按钮关闭键盘.任何人都可以提供解决方案吗?百万谢谢.

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:@"


"
                                                            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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:“没有 id 0 的键盘"- 这是什么意思? 下一篇:如何防止软件键盘弹出?

相关文章

最新文章