首页 文章

AppDelegate PUSH viewcontroller

提问于
浏览
0

AppDelegate - 如何 push 我的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([])
}

2 回答

  • 1

    首先,您应该尽可能保持AppDelegate的清洁 . 应用程序委托应该只处理应用程序启动时发生的事情 . 通常情况下,这只是设置初始窗口(即进入应用程序的内容) . 因此,您应该提出您的第一个视图控制器,并将其设置为AppDelegate中FinishedLaunching中的rootViewController和窗口 . 如果你想使用UINavigationController(听起来像你这样),你可以在UINavigationController( UINavigationController(FirstViewController()) )中嵌入你想要开始的ViewController,并将导航控制器设置为rootViewController .

    听起来你好像在UINavigationController中使用它一样 . 完成此步骤后,您应该只能使用show(nextViewController),它是您希望"push"来自UIViewController的函数 . 如果你想要的话 .

  • 0

    我的解决方案是复制ProductViewController并设置storyboard id . 在下面的代码中,在 withIdentifier 中编写新viewcontroller的故事板,在我的例子中是"NotificationProductDetailsViewController" . 别忘了在这个viewcontroller中添加Go Back Button .

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

相关问题