0

假设当用户按下按钮时,需要显示一条消息,然后显示一个模态视图控制器。我会写这样的东西:

- (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];
}

使用此代码,模态视图控制器根本不会显示

但是,如果我切换两个代码块(首先显示模态,然后显示警报),视图控制器将模态显示,并且警报显示在其上方,这正是我想要的。

我的问题是:为什么警报会阻止显示模态视图控制器?

请注意,在调用presentModalViewController之前,我不想等待警报被解除。


更新: 我发现我的生产代码比我第一次输入的要复杂一些。我更新了示例代码,然后终于找到了一个简单的示例来重现该问题。

请注意,在我的生产代码中,弹出窗口是从我没有任何对视图控制器的引用的地方显示的,这就是我使用[UIApplication sharedApplication].keyWindow.rootViewController 而不是直接引用视图控制器的原因

4

1 回答 1

0

我终于找到了答案。问题确实在这一行:

[[UIApplication sharedApplication].keyWindow.rootViewController
 presentModalViewController:picker animated:YES];

显示警报时, [UIApplication sharedApplication].keyWindow.rootViewController为 nil !. 这就是选择器从未显示的原因。这就是为什么当我切换两个块时它起作用的原因。

现在我需要找到一种方法来让最相关的视图控制器以模态方式呈现选择器......

于 2012-12-13T12:41:22.573 回答