【问题标题】:Open notification to specific view打开特定视图的通知
【发布时间】:2015-09-04 14:04:38
【问题描述】:

我正在使用推送通知在 Swift 中开发一个应用程序。到目前为止,我的 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(请参阅screenshot)。

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

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 填充它?

【问题讨论】:

    标签: ios swift push-notification apple-push-notifications


    【解决方案1】:

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

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

    【讨论】:

    • 我让它工作了。我用旅行列表视图和旅行视图实例化并填充了导航控制器(这样我就可以拥有后退按钮功能),然后我用导航控制器替换了 UIWindow 实例上的 rootViewController。
    猜你喜欢
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2020-09-30
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多