繁体   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