2015-09-04 80 views
3

我正在使用推送通知在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填充它?

回答

1

如果您使用UIStoryBoard並使用initialViewController,則iOS會自動執行需要的操作,即根據需要創建navigationController並將其加載到窗口中。

但是在這種情況下,您需要手動執行此操作。你需要創建一個UINavigationController,用你的TripViewController填充它,並用UIWindow掛鉤它。

+1

我得到它的工作。我實例化和填充導航控制器的旅程列表視圖和行程視圖(以便我可以有後退按鈕功能),然後我用導航控制器替換UIWindow實例上的rootViewController。 –

相關問題