从appdelegate推送根视图控制器

时间:2017-05-23 18:43:15

标签: ios swift controller viewcontroller

我的应用程序rootviewcontroller带有标签栏,但是当我使用firebase推送通知时,它只允许我"出现"方法。我希望在查看此视图​​控制器时使用push(如child)查看控制器以显示标签栏和后退选项。

在我的代码中,我想从现在转移到PUSH,但我无法找到解决方案。 currentController。现在(控制器,动画:真,完成:无)

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

    if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true {

        //TODO


            if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ProductDetailsViewController") as? ProductDetailsViewController {
                controller.productFromNotificationByCode(code: userInfo["product_code"] as! String, productID: userInfo["product_id"] as! String)
                if let window = self.window, let rootViewController = window.rootViewController {
                    var currentController = rootViewController
                    while let presentedController = currentController.presentedViewController {
                        currentController = presentedController
                    }
                    currentController.present(controller, animated: true, completion: nil)
                }
            }
        }
        print(userInfo)
    }
    completionHandler()
}

2 个答案:

答案 0 :(得分:1)

最简单的方法是将根视图控制器包装在故事板中的UINavigationController中。这将使实际的根控制器成为UINavigationController,而不是UITabBarController。

一旦完成,你可以简单地做

if let window = self.window, let rootNavController = window.rootViewController as? UINavigationController {
   navigationController?.pushViewController(controller, animated: true)
}

或者,如果您真正想要的是“从右侧滑动”动画并且不需要完整的导航堆栈,您可以编写自定义视图控制器转换,使用模态控制器提供类似的效果。 / p>

答案 1 :(得分:0)

我还没有测试过,但我相信它只是有效。试试这个:

func pushViewController() {

    let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

    let navigationController = storyboard.instantiateViewController(withIdentifier: "YourNavigationController") as! UINavigationController

    let productDetailsViewController = storyboard.instantiateViewController(withIdentifier: "ProductDetailsViewController") as! ProductDetailsViewController

    tabBarController.viewControllers = [navigationController]
    if tabBarController.selectedViewController == navigationController {
        navigationController.pushViewController(productDetailsViewController, animated: true)
    }



    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

}