简体   繁体   中英

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. I tryed this: "controller.navigationController?.pushViewController(controller, animated: true)" but It gives me fatal error, found nil while unwraping optional value. 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. 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. 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.

It sounds like you haven't done this step of embedding the view controller you're using in the 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. 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.

The solution for me was to duplicate the ProductViewController and set storyboard id. In the code below write the storyboard of the new viewcontroller in withIdentifier , in my case "NotificationProductDetailsViewController". Dont forget to add Go Back Button in this viewcontroller.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM