web-dev-qa-db-ja.com

appDelegateからUIAlertViewを提示する方法

アプリがプッシュ通知を受け取ったときに、didReceiveRemoteNotificationのappDelegateからUIAlertViewを表示しようとしています。

私はこのエラーを出しました:

  Warning: Attempt to present <UIAlertController: 0x14c5494c0> on <UINavigationController:
  0x14c60ce00> whose view is not in the window hierarchy!

これが私のコードです:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary) {

    var contentPush: NSDictionary = userInfo.objectForKey("aps") as NSDictionary

    var message = contentPush.objectForKey("alert") as String



    let alertController = UIAlertController(title: "Default Style", message: message, preferredStyle: .Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
                    // ...
    }
    alertController.addAction(cancelAction)

    let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        let photoPushedVc = self.storyboard.instantiateViewControllerWithIdentifier("CommentTableViewController") as CommentTableViewController

        println("the fetched post is \(post)")

        photoPushedVc.post = post

        let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController

        activeVc?.presentViewController(photoPushedVc, animated: true, completion: nil)
   }

   alertController.addAction(OKAction)

   let activeVc = UIApplication.sharedApplication().keyWindow?.rootViewController


   activeVc?.presentViewController(alertController, animated: true, completion: nil)}
14
jmcastel

OK、ついにそれを手に入れました。alertControllerを提示する前に、これを使用してアクティブなVCを見つける必要があります。

let navigationController = application.windows[0].rootViewController as UINavigationController
let activeViewCont = navigationController.visibleViewController
activeViewCont.presentViewController(alertController, animated: true, completion: nil)
13
jmcastel

ObjectD-Cを使用してAppDelegateからAlertControllerダイアログボックスを生成するには、

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Hello World!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];

タイプ1

UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

タイプ2

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];

どちらもtestedで、正常に動作しています。

17
computingfreak

これは私のものですSwift 3.0の例

func showTopLevelAlert() {
    let alertController = UIAlertController (title: "title", message: "message.", preferredStyle: .alert)

    let firstAction = UIAlertAction(title: "First", style: .default, handler: nil)
    alertController.addAction(firstAction)

    let cancelAction = UIAlertAction(title: "Отмена", style: .cancel, handler: nil)
    alertController.addAction(cancelAction)

    let alertWindow = UIWindow(frame: UIScreen.main.bounds)

    alertWindow.rootViewController = UIViewController()
    alertWindow.windowLevel = UIWindowLevelAlert + 1;
    alertWindow.makeKeyAndVisible()

    alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)

}

それが誰かに役立つことを願って

10
Nosov Pavel

Objective-cで同じものが必要な場合

UIAlertController *alertvc = [UIAlertController alertControllerWithTitle:@"Alert Title..!!" message:@"Hey! Alert body come here." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

}];

[alertvc addAction:actionOk];
  1. ナビゲーションベースのアプリがある場合:

    UINavigationController *nvc = (UINavigationController *)[[application windows] objectAtIndex:0].rootViewController;
    UIViewController *vc = nvc.visibleViewController;
    [vc presentViewController:alertvc animated:YES completion:nil];
    
  2. シングルビューベースのアプリがある場合:

    UIViewController *vc = self.window.rootViewController;
    [vc presentViewController:alertvc animated:YES completion:nil];
    
6
umakanta

どうやってやった

_func showAlertAppDelegate(title : String,message : String,buttonTitle : String,window: UIWindow){
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.Default, handler: nil))
    window.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
_

使用例

self.showAlertAppDelegate(title: "Alert",message: "Opened From AppDelegate",buttonTitle: "ok",window: self.window!);

ソースコードを含むサンプルをダウンロード

5
Munish Kapoor

アプリデリゲートからトップコントローラーにアラートを表示するには

var topController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((topController.presentedViewController) != nil) {
        topController = topController.presentedViewController!
    }

    //showAlertInViewController func is in UIAlertController Extension
    UIAlertController.showAlertInViewController(topController, withMessage: messageString, title: titleString)

UIAlertControllerに拡張機能を追加する

    static func showAlertInViewController(viewController: UIViewController?, withMessage message: String, title: String) {
    let myAlert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    myAlert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .Default, handler: { (action: UIAlertAction!) in
        print("Handle Ok logic here")

        // Let the alert simply dismiss for now
    }))

    viewController?.presentViewController(myAlert, animated: true, completion: nil)
}
0
Mohsin Qureshi

UIViewController拡張機能を使用して、現在表示されているビューコントローラーを取得します( ストーリーボードを使用しているときにアプリのデリゲートから表示されたviewControllerを取得する方法 )。

次にアラートコントローラを提示します。

let visibleVC = UIApplication.sharedApplication().keyWindow?.rootViewController?.visibleViewController
visibleVC!.presentViewController(alertController, animated: true, completion: nil)
0
MrRhoads