簡體   English   中英

從appdelegate推送根ViewController

[英]push root viewcontroller from appdelegate

我的應用程序rootviewcontroller帶有選項卡欄,但是當我使用Firebase推送通知時,它僅允許我使用“ present”方法。 當我查看此視圖​​控制器時,我想通過推(如子)查看控制器以具有選項卡欄和后退選項。

在我的代碼中,我想從現在轉移到PUSH,但找不到解決方案。 currentController。 目前 (控制器,動畫:true,完成:nil)

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

最簡單的方法是將根視圖控制器包裝在情節提要中的UINavigationController中。 這將使實際的根控制器成為UINavigationController而不是UITabBarController。

一旦完成,您就可以做

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

或者,如果您真正想要的只是一個“從右邊滑動”動畫,並且不需要完整的導航堆棧,則可以編寫一個自定義視圖控制器過渡,使用模態控制器可以達到類似的效果。

我尚未進行測試,但我相信它是可行的。 嘗試這個:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM