2017-08-29 73 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

相关问题