[英]Keyboard not dismissing when UIAlertView with textfields is dismissed
Ok, so I've got a view controller that is being displayed one of two ways: 好的,所以我有一个显示以下两种方式之一的视图控制器:
Basically, it's a settings view that can be shown via navigating to the settings section of the app or shown on demand if the user needs to input something into the view. 基本上,它是一个设置视图,可以通过导航到应用程序的设置部分来显示,或者如果用户需要在视图中输入某些内容,则可以按需显示。
In the settings view, I have a UIAlertView that pops up with a username and password style alert. 在设置视图中,我有一个UIAlertView,带有用户名和密码样式警报。 The first text field is set to firstResponder and the keyboard appears as normal.
第一个文本字段设置为firstResponder,键盘正常显示。 If I dismiss the alert (via cancel or any other button) the keyboard disappears.
如果我关闭了警报(通过“取消”或任何其他按钮),键盘将消失。
Now, for the issue: If I show the view controller through the segue, everything works properly. 现在,解决这个问题:如果我通过segue显示视图控制器,则一切正常。 If I show it manually, the keyboard will never dismiss no matter what I do.
如果我手动显示它,则无论我做什么,键盘都将永远不会消失。
I've tried setting another view as first responder then resigning, that doesn't help. 我尝试将其他视图设置为第一响应者,然后辞职,这无济于事。 I've tried calling [self.view endEditing:YES] but that doesn't help.
我尝试调用[self.view endEditing:YES],但这无济于事。 I've sent a global resignFirstResponder via:
我已通过以下方式发送了全球resignFirstResponder:
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
but that doesn't help. 但这无济于事。 I've even hooked up a private API to get the first responder, but it reported the first responder as (null).
我什至已经连接了私有API来获取第一个响应者,但是它报告第一个响应者为(null)。
Any ideas? 有任何想法吗?
You can conform self as a delegate of your UIAlertView once you create one. 创建一个UIAlertView后,您就可以将self作为其代表。 After that in:
在那之后:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
method you can get reference to your text field and you can call resignFirstResponder on it. 方法,您可以获取对文本字段的引用,并可以在其上调用resignFirstResponder。
I would make sure to show the alert late in the view life cycle of the view controller that's presenting it, eg 我要确保在呈现警报的视图控制器的视图生命周期中显示警报,例如
- (void)viewDidAppear {
[super viewDidAppear];
[self performSelector:@selector(showTheAlert) afterDelay:0.0];
}
- (void)showTheAlert {
// alloc, init and show the alert here
}
And I'd use a delegate callback to give the textView first responder, eg 我将使用委托回调给textView第一响应者,例如
- (void)didPresentAlertView:(UIAlertView *)alertView {
[theUsernameTextField becomeFirstResponder];
}
And I'd use the delegate for any dismissal to resign. 我会用委托人辞职。 There's no downside to resigning first responder on a text field that isn't the first responder, so just resign both.
在不是第一响应者的文本字段上辞退第一响应者没有任何弊端,因此只需将两者都辞职即可。
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
[theUsernameTextField resignFirstResponder];
[thePasswordTextField resignFirstResponder];
}
Implement the below method 实施以下方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *txtField = [alertView textFieldAtIndex:buttonIndex];
[txtField resignFirstResponder];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.