window.rootViewController不是当前的视图控制器

时间:2015-09-04 18:46:58

标签: ios swift apple-push-notifications

我正在尝试设置我的应用以支持推送通知,并且当推送通知打开应用时,我已经完成了所有工作,但现在我尝试添加以在应用时显示提醒已打开并收到通知。

到目前为止,我的AppDelegate中有以下代码:

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("Received alert and opened it")
    debugPrintln(userInfo)
    if let tripId = (userInfo["trip"] as? String)?.toInt() {
      if application.applicationState == UIApplicationState.Active { // App in foreground
        openTripFromForeground(tripId, type: userInfo["type"] as! String)
      } else { // App in background
        openTripFromBackground(tripId, type: userInfo["type"] as! String)
      }
    }
  }

  func openTripFromForeground(tripId: Int, type: String) {
    var alertController: UIAlertController!
    switch(type) {
      case "new":
        alertController = UIAlertController(title: "New trip", message: "You've been assigned to trip \(tripId).", preferredStyle: .Alert)
      case "changed":
        alertController = UIAlertController(title: "Changed trip", message: "Trip \(tripId) has changed.", preferredStyle: .Alert)
      case "removed":
        alertController = UIAlertController(title: "Removed from trip", message: "Trip \(tripId) has been removed.", preferredStyle: .Alert)
      default:
        println("Trip did something")
        return
    }
    if let rootViewController = self.window?.rootViewController {
      debugPrintln(rootViewController)
      rootViewController.presentViewController(alertController, animated: true, completion: nil)
    }
  }

要了解应用程序故事板的构建方式,请参阅this postthis screenshot

代码运行时,我在控制台中收到以下日志:

2015-09-04 13:57:05.878 GoDriver[1050:415300] Warning: Attempt to present <UIAlertController: 0x14f76130> on <GoDriver.LoginViewController: 0x14e37fd0> whose view is not in the window hierarchy!

即使可见视图是TripsTableViewController。

3 个答案:

答案 0 :(得分:1)

您似乎在UINavigationController之上提出LoginViewControllerrootViewController window LoginViewController的原因UIApplication每个故事板及其{窗口中未加载视图。

我在visiableViewControler上创建了扩展程序以获取extension UIApplication { class func visibleViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return visibleViewController(nav.visibleViewController) } if let tab = base as? UITabBarController { let moreNavigationController = tab.moreNavigationController if let top = moreNavigationController.topViewController where top.view.window != nil { return visibleViewController(top) } else if let selected = tab.selectedViewController { return visibleViewController(selected) } } if let presented = base?.presentedViewController { return visibleViewController(presented) } return base } }

visiableViewController

现在您需要从rootViewController window获取let visiableViewController = UIApplication.visibleViewController(rootViewController) visiableViewController.presentViewController(alertController, animated: true, completion: nil) 并在该viewController上显示您的alertView。

{{1}}

答案 1 :(得分:0)

我们在app delegate中做了类似的事情,

BOOL viewLoaded = navController.topViewController.isViewLoaded;
            if (viewLoaded)
            {
                [navController presentViewController:yourViewController animated:YES completion:nil];
            }
            else
            {
                self.showDeepLink = true;
            }

然后在主要活动中

if (delegate.showDeepLink)
    {
        delegate.showDeepLink = false;
        [self.navigationController presentViewController:yourViewController animated:YES completion:nil];
    }

答案 2 :(得分:0)

我已经使用这样的代码来呈现当前最顶层控制器的警报。也许它会对你有用吗?

- (void)handleError:(NSError *)error {
    NSBundle *mainBundle = [NSBundle mainBundle];

    NSString *title = [error localizedDescription];
    NSString *message = [[error userInfo] objectForKey:NSLocalizedFailureReasonErrorKey];

    // Find top-most controller
    UIViewController *viewController = [_window rootViewController];

    UIViewController *presentedViewController;
    do {
        presentedViewController = [viewController presentedViewController];

        if (presentedViewController != nil) {
            viewController = presentedViewController;
        }
    } while (presentedViewController != nil);

    // Display alert
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message  preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:[mainBundle localizedStringForKey:@"application.ok"] style:UIAlertActionStyleDefault handler:nil]];

    [viewController presentViewController:alertController animated:true completion:nil];
}