【问题标题】:Navigate to a Specific view on click on a push notification with Reveal点击带有 Reveal 的推送通知导航到特定视图
【发布时间】:2017-08-29 09:03:02
【问题描述】:

当我点击推送通知时,我想导航到特定的视图控制器。

我已经在我的 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)
}

但它给了我这个导致应用程序崩溃的错误。

致命错误:在展开可选值时意外发现 nil

有很多关于导航控制器或只是展示视图控制器的帖子。但我想通过显示导航到特定视图。

任何帮助将不胜感激。

【问题讨论】:

  • 您确定给定屏幕的情节提要 ID 是“deals”并且它具有 FYIDealsVC 类吗?
  • 是的,仅在这一行中它分解了 let naviVC:UINavigationController? = self.window?.rootViewController?.revealViewController().frontViewController 为? UINavigationController
  • 你确定你正在获取导航控制器的对象调试这一行我认为你在这一行 self.window?.rootViewController?.revealViewController().frontViewController 中得到了 nil 吗? UINavigationController
  • 嗨,是的,我在那一行得到了零
  • 我有另一个解决方案可以尝试使用 window.rootviewcontroller 或者您可以使用通知中心进行导航

标签: ios swift firebase push-notification


【解决方案1】:

这是另一个解决方案试试这个:-

第一个选项:-

    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)

   }

【讨论】:

  • 您好,感谢您的回答。第一个似乎不起作用,而关于第二个,它正在起作用。但是您只能将观察者添加到一个类中。否则会被多次调用。我需要在每个班级都调用它。
  • 仅在登陆屏幕中定义并确保观察者只应添加一次。使用 userdefault 设置布尔值在观察者添加时更改值。
  • 是的,但是如果用户在另一个类中,则该类无法捕获该通知并执行所需的功能。我想要任何类捕获通知,但只执行一次所需的功能。
  • 你在使用 SWrevelvc 吗?
  • 其实我也有这个问题。即使我在基本控制器上添加 NotificationCenter ,那么在这种情况下,当应用程序处于活动状态时它正在调用但无法导航到下一个屏幕,为此我已经为每个屏幕添加了观察者并从中删除了观察者视图将消失。但如果它是背景,那么它工作正常。
【解决方案2】:

当您强制打开 FYIDealsVC 时似乎会发生错误。 可以用吗

let vc = FYIDealsVC()

而不是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多