打开特定视图的通知 [英] Open notification to specific view

查看:24
本文介绍了打开特定视图的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用推送通知在 Swift 中开发一个应用程序.到目前为止,我的 AppDelegate 中有以下代码:

I'm working on an app in Swift using push notifications. So far I have the following code in my AppDelegate:

  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("Received alert and opened it")
    debugPrintln(userInfo)

    if application.applicationState == UIApplicationState.Active {
      // App in foreground
      println("App in foreground already")
    } else {
      // App in background
      if let tripId = (userInfo["trip"] as? String)?.toInt() {
        println(tripId)
        Trips.load(tripId) { (responseCode, trip) in
          debugPrintln("Got the trip")
          if let trip = trip {
            if let window = self.window {
              if let rootViewController = window.rootViewController {
                if let storyboard = rootViewController.storyboard {
                  let viewController = storyboard.instantiateViewControllerWithIdentifier("Trip") as! TripViewController
                  viewController.trip = trip
                  rootViewController.presentViewController(viewController, animated: true, completion: nil)
                } else {
                  println("No storyboard")
                }
              } else {
                println("No root view controller")
              }
            } else {
              println("No window")
            }
          }
        }
      } else {
        println("Failed to get trip id")
      }
    }
  }

故事板的构造是,当应用首次打开时,它会打开 LoginViewController,后者检查登录状态并重定向到包含行程列表的 NavigationController.从列表中,用户可以点击行程以打开 TripViewController(请参阅屏幕截图).

The storyboard is constructed that when the app first opens, it opens to the LoginViewController, which checks login state and redirects to a NavigationController containing a list of trips. From the list, a user can tap a trip to open the TripViewController (see screenshot).

当我运行我的应用程序并测试点击推送通知时,应用程序会加载行程列表,我会在控制台中看到以下日志:

When I run my app and test tapping on a push notification, the app loads the trip list and I see the following log in my console:

2015-09-04 09:50:07.158 GoDriver[883:377922] Warning: Attempt to present <GoDriver.TripViewController: 0x15f5b260> on <GoDriver.LoginViewController: 0x15d910e0> whose view is not in the window hierarchy!

我是否必须加载我的导航控制器并用 TripViewController 填充它?

Do I have to load up my Navigation Controller and populate it with the TripViewController?

推荐答案

如果您使用 UIStoryBoard 并使用 initialViewController,iOS 会自动执行所需的操作,即加载它, 如果需要,创建 navigationController 并将其加载到窗口.

If you are using UIStoryBoard and using the initialViewController, iOS automatically does the needful i.e loads it up, creates navigationController if needed and loads it to window.

但是在这种情况下,您需要手动执行此操作.你需要创建一个 UINavigationController,用你的 TripViewController 填充它,并用 UIWindow 钩住它.

However in this case you will need to do this bit manually. You would need to create a UINavigationController, populate it with your TripViewController and hook it with UIWindow.

这篇关于打开特定视图的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆