【问题标题】:AppDelegate PUSH viewcontrollerAppDelegate PUSH 视图控制器
【发布时间】:2017-10-25 09:40:21
【问题描述】:

AppDelegate - 如何push 我的viewcontroller 而不是present,原因是我想在我的下一个viewcontroller 上也有标签栏和导航栏。我试过这个:“controller.navigationController?.pushViewController(控制器,动画:真)”但它给了我致命的错误,在展开可选值时发现为零。有什么想法吗?

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

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    听起来您还没有完成将您正在使用的视图控制器嵌入到 UINavigationController 中的这一步。完成此步骤后,您应该可以使用show(nextViewController),它是您想要“推送”的 UIViewController 上的一个函数。如果您在 UINavigationController 中,这将执行 Push 动画,并且下一个视图将成为导航堆栈的一部分,就像您想要的那样。

    【讨论】:

      【解决方案2】:

      我的解决方案是复制 ProductViewController 并设置故事板 ID。在下面的代码中,在 withIdentifier 中编写新视图控制器的情节提要,在我的例子中是“NotificationProductDetailsViewController”。不要忘记在这个视图控制器中添加返回按钮。

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

      【讨论】:

        猜你喜欢
        • 2012-02-19
        • 2017-04-11
        • 2015-05-23
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 2015-10-20
        • 2018-12-25
        • 2016-11-13
        相关资源
        最近更新 更多