为什么UIAlertView阻止显示我的模式视图控制器? [英] Why UIAlertView prevents my modal view controller from being displayed?
问题描述
比方说,当用户按下按钮时,需要显示一条消息,然后显示一个模态视图控制器. 我会写这样的东西:
Let's say that when the user presses a button a message needs to be shown and then a modal view controller is displayed. I would write something like that :
- (void)pickImageButtonPressed:(id)button {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Some alert"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[[UIApplication sharedApplication].keyWindow.rootViewController
presentModalViewController:picker animated:YES];
[picker release];
}
使用此代码,模态视图控制器根本不会显示!
但是,如果我切换两个代码段(先显示模式,然后显示警报),则视图控制器将以模态显示,并且警报将完全按照我的意愿显示在其上方.
However, if I switch the two blocks of code (show modal first, then show the alert), the view controller is displayed modally and the alert shows up above it, exactly as I want.
我的问题是:为什么警报阻止显示模式视图控制器?
请注意,我不想在调用 presentModalViewController 之前等待警报消失.
Please note that I do not want to wait for alert to be dismissed before calling presentModalViewController.
更新: 我发现我的生产代码比我最初介绍的要复杂一些.我更新了示例代码,然后终于找到一个简单的示例来重现该问题.
Update : I figured out that my production code was a bit more complicated than what I first put. I updated the example code and then finally found a simple example to reproduce the problem.
请注意,在我的生产代码中,从没有任何对视图控制器的引用的地方显示弹出窗口,这就是为什么我使用 [UIApplication sharedApplication] .keyWindow.rootViewController而不是直接引用的地方到视图控制器
Note that in my production code, the popup is displayed from a place where I don't have any reference to a view controller, that's why I use [UIApplication sharedApplication].keyWindow.rootViewController instead of a direct reference to a view controller
推荐答案
我终于找到了答案.问题确实在这条线上:
I finally found the answer. The issue really was on this line:
[[UIApplication sharedApplication].keyWindow.rootViewController
presentModalViewController:picker animated:YES];
显示警报时,[UIApplication sharedApplication] .keyWindow.rootViewController 为空!.这就是为什么从不显示选择器的原因.这就是为什么当我切换两个模块时它起作用的原因.
When an alert is displayed, [UIApplication sharedApplication].keyWindow.rootViewController is nil !. That's why the picker was never displayed. And that's why it worked when I switched the two blocks.
现在,我需要找到一种方法,以获取最相关的视图控制器以模态方式呈现选择器...
Now I need to find a way to get the most relevant view controller to present the picker modally...
这篇关于为什么UIAlertView阻止显示我的模式视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!