简体   繁体   English

AppDelegate PUSH ViewController

[英]AppDelegate PUSH viewcontroller

AppDelegate - How to push my viewcontroller instead of present , the reason is that I want to have the tab bar on my next viewcontroller with navigation bar too. AppDelegate-如何推送我的viewcontroller而不是present ,原因是我也希望在我的下一个viewcontroller上具有导航栏的标签栏。 I tryed this: "controller.navigationController?.pushViewController(controller, animated: true)" but It gives me fatal error, found nil while unwraping optional value. 我尝试了这个:“ controller.navigationController?.pushViewController(controller,animated:true)”,但是它给了我致命错误,在取消包装可选值时发现nil。 Any idea? 任何想法?

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

First off, you should keep your AppDelegate as clean as possible. 首先,您应该保持AppDelegate尽可能干净。 The app delegate should really only handle things that are happening right when the app launches. 应用程序委托实际上应该只处理应用程序启动时正在发生的事情。 More often than not, that's just going to be setting your initial window (ie what will be the entrance into your app). 通常,这只是设置您的初始窗口(即,进入应用程序的入口)。 So you should come up with whatever your first view controller and set that to your rootViewController and window in FinishedLaunching in the AppDelegate. 因此,您应该使用第一个视图控制器,然后将其设置为AppDelegate的FinishedLaunching中的rootViewController和窗口。 If you want to use a UINavigationController (sounds like you do) you can embed the ViewController you want to start with in the UINavigationController ( UINavigationController(FirstViewController()) ) and set the navigation controller to the rootViewController. 如果要使用UINavigationController(听起来很像),可以将要开始使用的ViewController嵌入UINavigationController( UINavigationController(FirstViewController()) )中,并将导航控制器设置为rootViewController。

It sounds like you haven't done this step of embedding the view controller you're using in the UINavigationController. 听起来您还没有完成将要使用的视图控制器嵌入UINavigationController中的步骤。 Once you've done this step, you should be able to just use show(nextViewController) which is a function on the UIViewController that you want to "push" from. 完成此步骤后,您应该可以仅使用show(nextViewController) ,这是您要从中“推送”的UIViewController上的函数。 If you're in a UINavigationController, this will do the Push animation and the next view will be part of the navigation stack, like you're wanting. 如果您在UINavigationController中,则将执行Push动画,并且下一个视图将成为您想要的导航堆栈的一部分。

The solution for me was to duplicate the ProductViewController and set storyboard id. 对我来说,解决方案是复制ProductViewController并设置情节提要ID。 In the code below write the storyboard of the new viewcontroller in withIdentifier , in my case "NotificationProductDetailsViewController". 在下面的代码中,以withIdentifier编写新视图控制器的情节提要 ,在我的案例中为“ NotificationProductDetailsViewController”。 Dont forget to add Go Back Button in this viewcontroller. 不要忘记在此视图控制器中添加“返回按钮”。

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