1

今までUIAlertViewを使用していたのですが、
これをUIAlertControllerに一律置き換えています。

そこで、AppDelegate上にあるAlertViewを
どのように置き換えるかという点について教えてください。
(ViewControllerではないので、presentViewControllerができずにいます)

2 件の回答 2

1

以前qiitaへ投稿した記事からの抜粋です。
http://qiita.com/takabosoft/items/162b6d90e00b79a7e328

どこから呼び出しても(各種モーダルビューが出ていても)大丈夫なように作ったつもりです。

// 警告を表示します。OKボタンタップで閉じます。
void Warning(NSString *message) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];

    // 親ビューコンをなんとか検索
    UIViewController *baseView = [UIApplication sharedApplication].keyWindow.rootViewController;
while (baseView.presentedViewController != nil && !baseView.presentedViewController.isBeingDismissed) {
    baseView = baseView.presentedViewController;
    }
    [baseView presentViewController:alert animated:YES completion:nil];
}

ご参考まで。

1
  • ご回答ありがとうございます。 こちらで無事に解決する事ができました! 助かりました。
    – GQ34
    2015年5月21日 9:27
1

AppDelegateクラスは、テンプレートから変更していなければ、windowというプロパティがあります。それを前提にし、Alert Controllerのインスタンス名をalertControllerとすると、

[self.window.rootViewController presentViewController: alertController animated: YES completion: NULL];

ただし、self.window.rootViewControllerが、現在画面に表示されているView Controllerでないかもしれないので、そこは注意が必要でしょう。

1
  • ご回答ありがとうございます。 こちらで取得する事ができました。 ただ、やはり現状の画面と一致しておらず、取得する為の仕組みを考えてました。 今回はこれで解決ができませんでしたが、今回の用途でなければ、これで大丈夫かも知れません。
    – GQ34
    2015年5月21日 9:31

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。