gpt4 book ai didi

iphone - ResignFirstResponder 不会关闭键盘 (iPhone)

转载 作者:行者123 更新时间:2023-12-03 18:39:36 25 4
gpt4 key购买 nike

我在这个网站上搜索过,但找不到解决我现在面临的问题的方法。希望有人能帮忙。

我创建了一个 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,并实现了 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;
}

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

最佳答案

首先,您需要在界面 header 中定义您的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;
}

关于iphone - ResignFirstResponder 不会关闭键盘 (iPhone),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3346023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com