在录制通知时显示ViewController

时间:2017-06-02 07:47:45

标签: ios swift

当用户点按本地通知时,我正在尝试显示详细视图控制器。 到目前为止,我有这个:

//AppDelegate.swift

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let id = userInfo["item_id"] as? Int {
        if let item = ItemsRepository.shared.getItem(id: id) {
            let vc = DetailTableViewController()
            vc.item = item
            let tabController = self.window?.rootViewController as! UITabBarController
            let navigationController = tabController.selectedViewController as! UINavigationController
            navigationController.pushViewController(vc, animated: true)
        }
    }
    completionHandler()
}

此代码由于某种原因生成异常:

libc++abi.dylib: terminating with uncaught exception of type NSException

  • 第2版

`

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let id = userInfo["item_id"] as? Int {
        if let item = ItemsRepository.shared.getItem(id: id) {
            let vc = DetailTableViewController()
            vc.item = item
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "DetailView") as! DetailTableViewController
            vc.item = item
            window?.rootViewController = vc
            self.window?.makeKeyAndVisible()

        }
    }
    completionHandler()
}

它有点用,但标签栏和导航控制器不再可见!

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

而不是let vc = DetailTableViewController()

试试这个

    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "someViewController")

告诉我这是否有效

答案 1 :(得分:0)

试试这个

            let tabController = self.window?.rootViewController as! UITabBarController
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "DetailView") as! DetailTableViewController
            vc.item = item
            window?.rootViewController = tabController
            tabController.present(vc, animated: true, completion: nil)

答案 2 :(得分:0)

我已经通过跟踪故事板文件上的屏幕流程解决了我的问题。基本上,我实例化了我的Main.storyboard,然后是后面的每个视图控制器。 storyboard

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let id = userInfo["item_id"] as? Int {
        if let item = ItemsRepository.shared.getItem(id: id) {
            let dv = DetailTableViewController()
            dv.item = item
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let tabbar = storyboard.instantiateViewController(withIdentifier: "homeFeed") as! UITabBarController
            tabbar.selectedIndex = 0
            let nav = tabbar.selectedViewController as! UINavigationController
            let main = nav.topViewController as! HomeCollectionViewController
            main.performSegue(withIdentifier: "ShowDetailSegue", sender: item)
            self.window?.rootViewController = tabbar
            window?.makeKeyAndVisible()

        }
    }
    completionHandler()
}