2014-11-15 72 views
33

我試圖在iOS應用程序中從AppDelegate提供UIAlertController。 以下是警報和現在的方法。從AppDelegate提供UIAlertController

UIAlertController *alert = [UIAlertController alertControllerWithTitle:cTitle message:cMessage preferredStyle:UIAlertControllerStyleAlert]; 

//Configure alert and actions 

[self.window.rootViewController presentViewController:alert animated:TRUE completion:nil]; 

但是,當我嘗試顯示警報時,它不會顯示,並且在控制檯中會顯示以下警報。

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

什麼是導致錯誤,我該如何解決它?

+0

試圖呈現右爲應用程序正在啓動我猜? – Acey

+0

你可以嘗試這個鏈接有用鏈接:[alertViewController](http://stackoverflow.com/a/37029075/2442762) –

回答

5

你怎麼稱呼它之前的窗口,並且它的navigationController實際上顯示:

「警告:試圖提出有關誰的觀點是不是在窗口層次結構」

你有可能在applicationDidFinishLaunching中這麼做嗎?


要麼等待..喜歡做時,認爲真的出現

OR

一個 '黑客' 將迫使視圖和窗口了自己:

[self.window addSubview:self.rootViewController.view]; 
[self.window makeKeyAndVisible]; 
+0

如果你不是*創建*一個新的窗口,你還需要做'makeKeyAndVisible '?對於AppDelegate附帶的窗口,默認情況下不會發生?! – Honey

+0

一般情況:是的,但是對於op的問題發生得太晚了 –

+0

我很困惑。你什麼意思太晚了?你的代碼是否正確? – Honey

1

您可以嘗試在viewDidAppear而不是viewDidLoad中調用它。我有一個類似的錯誤,並修復它。

27

如果你想從didFinishLaunchingWithOptions.Hope啓動它,你可以使用這段代碼。

dispatch_async(dispatch_get_main_queue(), { 
       let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) 
      self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil) 
     }) 
+0

嗨@AdiQuadCore,你只在快速應用程序中有用。在objC中,'UIAlertController'需要一個指向當前'UIViewController'的指針。 – Zeb

+1

這在ObjC中適用於didFinishLaunchingWithOptions – Seoras

9

這是更好地使用這樣的:

var hostVC = self.window?.rootViewController 

while let next = hostVC?.presentedViewController { 
    hostVC = next 
} 

hostVC?.presentViewController(alertController, animated: true, completion: nil) 

以前的答案,如果你已經呈現模式視圖控制器將無法工作。

+0

完美的答案和一個非常好的方法來處理模態顯示的視圖控制器!謝謝! – NerdyTherapist

+0

很好,謝謝。 +1 – drct

-1

使用警報控制器:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
dispatch_async(dispatch_get_main_queue(), { 

    //INTERNET NOT AVAILABLE ALERT 
    var internetUnavailableAlertController = UIAlertController (title: "Network Unavailable", message: "Please check your internet connection settings and turn on Network Connection", preferredStyle: .Alert) 

    var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in 
     let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString) 
     if let url = settingsUrl { 
      UIApplication.sharedApplication().openURL(url) 
     } 
    } 
    var cancelAction = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
    internetUnavailableAlertController .addAction(settingsAction) 
    internetUnavailableAlertController .addAction(cancelAction) 
    self.window?.rootViewController!.presentViewController(internetUnavailableAlertController , animated: true, completion: nil) 
    }) 

使用AlertView:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    dispatch_async(dispatch_get_main_queue(), { 

     //INTERNET NOT AVAILABLE ALERTVIEW 

     let internetUnavailableAlert = UIAlertView(title: "Network Unavailable", message: "Please check your internet connection settings and turn on Network Connection", delegate: self, cancelButtonTitle: "Okay") 
     internetUnavailableAlert.show() 
     }) 

    return true 
} 
} 
+0

UIAlertView現已棄用。 –

17

試試這個代碼..

-(void)application:(UIApplication *)application    didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
NSLog(@"rakshapettu"); 
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertView" message:@"I am an AlertView" preferredStyle:UIAlertControllerStyleAlert]; 
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault 
                 handler:^(UIAlertAction * action) { 
                  [alert dismissViewControllerAnimated:YES completion:nil]; 
                 }]; 
[alert addAction:defaultAction]; 
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
alertWindow.rootViewController = [[UIViewController alloc] init]; 
alertWindow.windowLevel = UIWindowLevelAlert + 1; 
[alertWindow makeKeyAndVisible]; 
[alertWindow.rootViewController presentViewController:alert animated:YES completion:nil]; 
} 
+1

「rakshapettu」 - 哈哈 –

2

我試圖相同,但不改變之後,因爲工作viewcontroller仍然返回初始viewcontroller並拋出錯誤whose view is not in the window hierarchy!。最後,我找到了解決這個問題:

UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

但我必須強迫視圖控制器去keyWindow與此更新:

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated) 
    UIApplication.sharedApplication().keyWindow!.rootViewController = self 
    UIApplication.sharedApplication().keyWindow!.makeKeyAndVisible() 
} 
+0

你不應該那樣做,真的。 – nobre