2017-08-29 72 views
0

當我單擊推送通知時,我想導航到特定的視圖控制器。導航到單擊具有Reveal的推送通知時的特定視圖

我在didReceiveRemoteNotification方法中編寫了這段代碼。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC 
     let naviVC:UINavigationController? = self.window?.rootViewController?.revealViewController().frontViewController as? UINavigationController 
     naviVC?.pushViewController(vc, animated: true) 
} 

但它給我這個崩潰的應用程序的錯誤。

fatal error: unexpectedly found nil while unwrapping an Optional value

有很多帶有導航控制器的帖子或只是提供了viewcontroller。但我想用揭示導航到具體的視圖。

任何幫助將不勝感激。

+1

你確定給定屏幕的故事板ID是 「交易」,它具有類'FYIDealsVC'? – the4kman

+0

是的,在這一行中只有它分解了 讓naviVC:UINavigationController? = self.window?.rootViewController?.revealViewController()。frontViewController as? UINavigationController – NavodaP

+0

你確定你正在獲取導航控制器的對象調試這一行我認爲你在這一行self.window?.rootViewController?.revealViewController()。 UINavigationController – Pushpendra

回答

2

這裏是這一個又一個的解決方案試試這個: -

第一個選項: -

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewController(withIdentifier: 
    "deals") as! FYIDealsVC 

    // setup revelview controller and root with window 

    self.window?.rootViewController = //object of revelViewController 
    self.window?.makeKeyAndVisible() 

第二個選項: -

// in Landing Screen from where you can easily navigate to the target 
    viewcontroller :- 

在着陸VC: -

viewdidload:- 
      NotificationCenter.default.addObserver(self, selector: 
    #selector(self.navigationForNotification(notification:)), name: 
    NSNotification.Name(rawValue:PushNavigationIdentifier), object: nil) 


// Selector Method :- 

func navigationForNotification(notification:Notification) { 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc = storyboard.instantiateViewController(withIdentifier: 
"deals") as! FYIDealsVC 
self.navigationController?.pushViewController(vc, animated: true) 
} 


in appDelegate :- 
    func application(_ application: UIApplication, 
    didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
        NotificationCenter.default.post(name: 
    NSNotification.Name(PushNavigationIdentifier), object: userInfo) 

    } 
+0

嗨感謝您的答案。第一個似乎沒有工作 和關於第二,它的工作。但是你可以只將觀察者添加到一個類中。否則它會被多次調用。我需要它在每個班級中被調用。 – NavodaP

+0

僅在登陸屏幕中定義,並確保觀察者只應添加一次。設置用戶默認的布爾當添加觀察者時更改該值。 – Pushpendra

+0

如果您的問題得到解決,請接受我的回答。 – Pushpendra

-2

嘗試刪除'as! FYIDealsVC',因爲您已經通過其標識符(交易)識別了您的視圖控制器。確保這個標識符在storyboardID中設置。我只是假設你的錯誤沒有指定發生崩潰的位置。

另外,如果你的看法被嵌入導航控制器,它足以寫「self.navigationController .pushViewController(VC,動畫:真)?」

+0

嗨,這不適合我,謝謝反正 – NavodaP

0

看來,當你用力展開FYIDealsVC像錯誤發生。 你可以使用

let vc = FYIDealsVC() 

,而不是

let vc = storyboard.instantiateViewController(withIdentifier: "deals") as! FYIDealsVC

+0

嗨,這不適合我,謝謝反正 – NavodaP

相關問題