ResignFirstResponder 不会关闭键盘 (iPhone)

ResignFirstResponder doesn#39;t dismiss the keyboard (iPhone)(ResignFirstResponder 不会关闭键盘 (iPhone))
本文介绍了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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

No resource found that matches the given name: attr #39;android:keyboardNavigationCluster#39;. when updating to Support Library 26.0.0(找不到与给定名称匹配的资源:attr android:keyboardNavigationCluster.更新到支持库 26.0.0 时) - IT屋-程序员软
How to resize UITextView on iOS when a keyboard appears?(出现键盘时如何在iOS上调整UITextView的大小?)
How to reliably detect if an external keyboard is connected on iOS 9?(如何可靠地检测 iOS 9 上是否连接了外部键盘?)
How do I dismiss the iOS keyboard?(如何关闭 iOS 键盘?)
Is possible to simulate touch event using an external keyboard on ios jailbroken?(是否可以在 ios 越狱后使用外部键盘模拟触摸事件?)
Numeric Soft Keyboard on Android(Android 上的数字软键盘)