2015-09-01 70 views
0

我有一個應用程序設置爲接收推送通知。當用戶收到通知時,我在我的appDelegate中有一個回調。如果應用程序處於非活動狀態並且用戶單擊設備面板上的通知,我需要能夠從此處繼續。從一個視圖控制器的appDelegate

該應用程序的流程是一個登錄視圖控制器(如果loginBool爲true,則跳過),這會導致製表符控制器。在選項卡控制器上,我有3個地方可以繼續使用id爲「FeedDetailedController」的相同viewController。

這是FeedDetailedController我需要繼續並傳遞一個變量,我收到我的通知。這個控制器可以從3個不同的地方訪問,其中2個是帶有表格視圖的標籤,當你點擊一行時,它傳入一個變量並執行一個繼續。我需要從我的應用程序委託中傳遞這個數據,即從通知中傳遞數據,就像我在該行中所做的一樣。

嘗試至今:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
     println("received a notification") 
     PFPush.handlePush(userInfo) 
     if application.applicationState == UIApplicationState.Inactive { 
      println("in the notification if with \(userInfo)") 

      if let info = userInfo["custom"] as? Dictionary<String, AnyObject> { 
       if let reportId = info["reportId"] as? String { 
        println("\nFrom APS-dictionary with key \"type\": \(reportId)") 
        //pass in reportId to the viewcontroller somehow 

        let storyboard = UIStoryboard(name: "Main", bundle: nil) 
        let vc = storyboard.instantiateViewControllerWithIdentifier("NewFeedDetailedController") as! UIViewController 

        let navigationController = UINavigationController(rootViewController: vc) 
        self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: nil) 
       } 

      } 
      PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo) 
     } 
     else{ 
      println("in the notification else") 
      //this is when the app is active, do I need to detect which view controller I am currently on before I can seg??? 
     } 
    } 

當前的代碼提供了以下消息:

Warning: Attempt to present <UINavigationController: 0x12ed763d0> on <UINavigationController: 0x12ed11ce0> whose view is not in the window hierarchy! 

這是有道理的,但我不知道我應該如何得到正確的層次結構來自哪裏appDelegate代碼

回答

0

是否可以在應用的初始視圖控制器中檢查該通知?就像將它作爲布爾值傳遞給通知一樣?你可以在viewWillAppear的地方找到它:並檢查它是否存在 - 如果它在那裏從該視圖控制segue,而不是試圖在appDelegate本身中執行它?

這樣它看起來會直接進入新的視圖,但真的要通過視圖控制器的層次結構加載到它?

+0

嗯,我不能將didReceiveRemoteNotification func移動到視圖控制器中。如果你要在應用程序委託中設置一個NSUserDefault Bool,然後檢查loginviewController是否收到通知,如果該標誌設置爲true,則繼續檢查,這可能會變得複雜,因爲我已經有一個loginBool跳過該屏幕,如果用戶登錄,我會觸發標籤欄的打開。但從通知移動到我想要的控制器將需要打開標籤欄,然後seguing到我的feedDetailedController? – user2363025

+0

我可以看到複雜性的危險,因爲你有邏輯來繞過基於該布爾的初始視圖。我不確定是否需要打開標籤欄以瞭解您需要做的事情。我基本上推薦使用通知設置的全局變量來告訴你在視圖控制器中需要知道什麼,然後觸發一個segue。從你所說的話看來,由於你的登錄邏輯你必須在兩個控制器中檢查這個......所以我不確定是否由於你對複雜性的關注而是「最好的」答案(正確的)。 – Charlie

+0

如果我把我的支票放在viewDidAppear的每個選項卡中並從那裏執行seg並重置bool,那麼怎麼辦?爲了解釋更多,我的應用程序中有5個選項卡。選項卡3有一個tableview,當點擊時會跳轉到前面描述的feedDetailedController。如果應用程序處於後臺,並且在重新打開時出現在選項卡2上,我會檢查viewDidAppear中的notificationBool,然後以編程方式繼續執行feedDetailedController。那麼當用戶按下時,他們只是返回到標籤二?或者我將不得不'保持選項卡'的地方發生並以編程方式繼續回到那個屏幕? – user2363025

相關問題