打开多个UIAlertViews后键盘关闭 [英] Keyboard dismisses after multiple UIAlertViews are opened

查看:88
本文介绍了打开多个UIAlertViews后键盘关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,我的键盘在我打开多个UIAlertViews时自动关闭然后重新打开.如果我有键盘(来自单独的UITextField)并且显示UIAlertView,则在解除该警报后,我将打开另一个警报(在didDismissWithButtonIndex中打开第二个警报).当我关闭第二个键盘时,它关闭了键盘,然后又弹出了.如果我连续尝试超过2个警报,则在关闭第二个警报后,它仍会关闭键盘,但直到最后一个警报消失后,它才会显示.问题在于未调用键盘委托函数,因此我无法响应被解雇的情况.我还有其他UI元素(文本字段和图像),它们在键盘打开时会移动,因此在关闭时,这些元素会浮在屏幕上,看起来很奇怪.知道为什么键盘会自动关闭吗?谢谢

I am having a weird issue with my keyboard automatically closing and then reopening when I open multiple UIAlertViews. If I have a keyboard (from a separate UITextField) and I display a UIAlertView, then upon dismissal of that alert I open another (opening the second one in the didDismissWithButtonIndex). When I dismiss the 2nd one it dismisses the keyboard which then comes back up. If i try this with more than 2 alerts in a row, it will still close my keyboard after the 2nd alert is dismissed, but it doesn't show up until the last alert is dismissed. The problem is that the keyboard delegate functions are NOT called so I cannot respond to it being dismissed. I have other UI elements (textfield and images) that get shifted when the keyboard opens, so when it closes those elements float in the screen and look strange. Any idea why that keyboard automatically dismisses? Thanks

顺便说一句,我使用NSDictionary对象的NSMutableArray来使需要显示的警报排队(如果已经显示警报的话).我一次不会创建和显示多个警报.

BTW, I use an NSMutableArray of NSDictionary objects to queue up alerts that need to be displayed if an alert is already displayed. I am not creating and displaying more than 1 alert at a time.

这是示例代码.如果运行此命令,则关闭"1"后,将同时打开两个警报(从0到1),您将在其下看到"0".取消为"0"后,您会明白我的意思-他们的键盘会短暂地关闭和打开,但不会调用任何委托函数.如果将i设置为大于2的值,您会看到在消除第二个警报后键盘仍然关闭,但是将保持关闭状态,直到最后一个警报被消除为止.我还尝试只打开1个UIAlert,然后一次从队列中打开另一个UIAlert,因为每个UIAlert都被解雇了,但仍然注意到相同的行为.有什么想法吗?

Here's sample code. If you run this you'll see both alerts open (0 then 1) after you dismiss '1' you'll see '0' under it. After you dismiss '0' you'll see what I mean - they keyboard briefly closes and opens but no delegate functions are called. If you set i to a value higher than 2, you'll see that the keyboard still closes after dismissing the 2nd alert, but will stay closed until the last alert is dismissed. I also tried opening just 1 UIAlert, and opening the others one at a time from a queue as each one was dismissed, and still noticed that same behavior. Any ideas?

经过更多的挖掘后,我发现如果我注册通知UIKeyboardDidShowNotification和UIKeyboardDidHideNotification,则实际上它们会在自动关闭并显示键盘时触发.我仍然想知道底层API是什么导致了它的发生,因此希望可以避免它.

After some more digging I found that if I register for the notifications UIKeyboardDidShowNotification and UIKeyboardDidHideNotification they are in fact fired when the keyboard is automatically dismissed and presented. I still would like to know what in the underlying API is causing it to even happen so it can hopefully be avoided.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
// Override point for customization after application launch.
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 320, 48)];
[textField setDelegate:self];
[textField setBackgroundColor:[UIColor redColor]];
[window addSubview:textField];
[textField release];
[self.window makeKeyAndVisible];
return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *) textField{
NSLog(@"textFieldShouldReturn called with %@", textField);
[textField resignFirstResponder];
return YES;
}


-(void) textFieldDidBeginEditing:(UITextField *)textField 
{
NSLog(@"textFieldDidBeginEditing called with %@", textField);
for (int i=0; i< 2; i++) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"test" message: [NSString stringWithFormat:@"%d", i] delegate:self cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"++++ textFieldShouldEndEditing %@", textField);
return YES;
}

-(void) textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"++++ textFieldDidEndEditing %@", textField);

}

推荐答案

仅当相应的UI元素是第一个响应者时才显示键盘.某种程度上,多个警报视图会在短时间内修改响应者链.似乎是一个框架问题.

the keyboard is only shown when the corresponding UI element is the first responder.. somehow multiple alert views modify the responder chain for a short time. Seems like a framework issue..

我建议这种解决方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        /* show new alert view here */
    });
}

编辑

实际上,我现在认为它与应用程序的窗口层次结构有关. UIAlertViews创建它们自己的窗口(在窗口级别UIWindowLevelAlert),将其设置为接收触摸输入的键窗口,然后在关闭时再次使旧的窗口键窗口.当您在didDismiss上显示新的警报视图时,UIKit似乎会丢失(暂时)对关键窗口和响应者链的跟踪.
上面的修复程序当然仍然适用.

actually i now think it has to do with the Window hierarchy of the application. UIAlertViews create their own window (at window level UIWindowLevelAlert), make them the key window to recieve touch input and then make the old window key window again upon dismissal. When you show a new alert view on didDismiss, UIKit seems to lose (temporarily) track of key windows and the responder chain..
The fix above of course still applies.

这篇关于打开多个UIAlertViews后键盘关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆