ユーザーがボタンを押したときにメッセージを表示する必要があり、モーダル ビュー コントローラーが表示されるとします。私はそのようなものを書きます:
- (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];
}
このコードでは、モーダル ビュー コントローラーが表示されません。
ただし、コードの 2 つのブロックを切り替えると(最初にモーダルを表示してからアラートを表示する)、View Controller がモーダルで表示され、その上にアラートが表示されます。
私の質問は、なぜアラートがモーダルView Controllerの表示を妨げているのですか?
presentModalViewControllerを呼び出す前にアラートが消えるのを待ちたくないことに注意してください。
更新 : 私のプロダクション コードは、最初に書いたコードよりも少し複雑であることがわかりました。サンプル コードを更新し、最終的に問題を再現する簡単な例を見つけました。
私のプロダクション コードでは、View Controller への参照がない場所からポップアップが表示されることに注意してください。そのため、View Controllerへの直接参照の代わりに [UIApplication sharedApplication].keyWindow.rootViewControllerを使用します。