首页 文章

从appdelegate推送根视图控制器

提问于
浏览
0

我的应用程序rootviewcontroller带有标签栏,但是当我使用firebase推送通知时,它只允许我使用“present”方法 . 当我查看此视图控制器时,我想用push(像child)查看控制器以显示标签栏和后退选项 .

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

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 回答

  • 1

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

    一旦完成,你就可以做到

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

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

  • 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()
    
    }
    

相关问题