簡體   English   中英

AppDelegate PUSH ViewController

[英]AppDelegate PUSH viewcontroller

AppDelegate-如何推送我的viewcontroller而不是present ,原因是我也希望在我的下一個viewcontroller上具有導航欄的標簽欄。 我嘗試了這個:“ controller.navigationController?.pushViewController(controller,animated:true)”,但是它給了我致命錯誤,在取消包裝可選值時發現nil。 任何想法?

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    if Defaults.hasKey(.logged), let logged = Defaults[.logged], logged == true {

    let userInfo = notification.request.content.userInfo
    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
        print("Message ID: \(messageID)")

        let topWindow = UIWindow(frame: UIScreen.main.bounds)
        topWindow.rootViewController = UIViewController()
        topWindow.windowLevel = UIWindowLevelAlert + 1
        let alert = UIAlertController(title: userInfo["title"] as? String, message: userInfo["body"] as? String, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: NSLocalizedString("Show Product", comment: "confirm"), style: .default, handler: {(_ action: UIAlertAction) -> Void in

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

        }))
        alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "cancel"), style: .destructive, handler: {(_ action: UIAlertAction) -> Void in
            topWindow.isHidden = true
        }))
        topWindow.makeKeyAndVisible()
        topWindow.rootViewController?.present(alert, animated: true, completion: { _ in })
    }
  }
    completionHandler([])
}

首先,您應該保持AppDelegate盡可能干凈。 應用程序委托實際上應該只處理應用程序啟動時正在發生的事情。 通常,這只是設置您的初始窗口(即,進入應用程序的入口)。 因此,您應該使用第一個視圖控制器,然后將其設置為AppDelegate的FinishedLaunching中的rootViewController和窗口。 如果要使用UINavigationController(聽起來很像),可以將要開始使用的ViewController嵌入UINavigationController( UINavigationController(FirstViewController()) )中,並將導航控制器設置為rootViewController。

聽起來您還沒有完成將要使用的視圖控制器嵌入UINavigationController中的步驟。 完成此步驟后,您應該可以僅使用show(nextViewController) ,這是您要從中“推送”的UIViewController上的函數。 如果您在UINavigationController中,則將執行Push動畫,並且下一個視圖將成為您想要的導航堆棧的一部分。

對我來說,解決方案是復制ProductViewController並設置情節提要ID。 在下面的代碼中,以withIdentifier編寫新視圖控制器的情節提要 ,在我的案例中為“ NotificationProductDetailsViewController”。 不要忘記在此視圖控制器中添加“返回按鈕”。

if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NotificationProductDetailsViewController") as? ProductDetailsViewController {

暫無
暫無

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

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