实例化vc,保持导航栏为Swift

问题描述 投票:0回答:1

我想在响应远程通知动作时显示NewMapViewController,但是如果我直接实例化NewMapViewController,则会松开导航栏。如果我改为实例化navigationController并执行segue,那么我看不到所有视图控制器(启动和登录)一个接一个地显示,这是非常不愉快的。有没有一种方法可以将导航栏保持在NewMapViewController中,并避免级联显示所有视图控制器?一如既往,非常感谢您的帮助。

这是我尝试过的:

case Id.checkActionIdentifier:
            print("Check tapped")
            let userInfo = response.notification.request.content.userInfo
            if let routeToCheck = userInfo[NSLocalizedString("Route", comment: "")] as? String {
                RoutesArray.routeName = routeToCheck
//                print("rotta is: \(routeToCheck)")
            }
//            let content = response.notification.request.content
//            print(" Body \(content.body)")
//            print(" Title \(content.title)")
            NewMapViewController.routeCheckCounter = 1

            //  Goes to NewMapViewController but showing the splash and login VC first
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController : UINavigationController = storyboard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController
            self.window?.rootViewController = initialViewController
            self.window?.makeKeyAndVisible()
            let vc = initialViewController.viewControllers[0]
            vc.performSegue(withIdentifier: "alertToMapSegue", sender: nil)

            // goes straight to NewMapViewController but looses nav bar
//            let storyboard = UIStoryboard(name: "Main", bundle: nil)
//            let initialViewController : UIViewController = storyboard.instantiateViewController(withIdentifier: "NewMapViewController") as! NewMapViewController
//            self.window?.rootViewController = initialViewController
//            self.window?.makeKeyAndVisible()

更新:

我已经能够避免通过登录vc,但是在NewMapViewController之前仍显示启动画面,并显示代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "NewMapViewController") as UIViewController
            self.window?.rootViewController?.show(vc, sender: self)
ios swift uinavigationcontroller
1个回答
1
投票

[似乎windowrootViewController已经是UINavitationController,因此您只需按以下方法抓取并按下NewMapViewController

 case Id.checkActionIdentifier:
     let userInfo = response.notification.request.content.userInfo
     if let routeToCheck = userInfo[NSLocalizedString("Route", comment: "")] as? String {
         RoutesArray.routeName = routeToCheck
     }
     NewMapViewController.routeCheckCounter = 1

     if let navigationVC = self.window?.rootViewController as? UINavigationController {
         let main = UIStoryboard(name: "Main", bundle: nil)
         let newMapVC = main.instantiateViewController(withIdentifier: "NewMapViewController") as! NewMapViewController
         navigationVC.pushViewController(newMapVC, animated: true)
     }
© www.soinside.com 2019 - 2024. All rights reserved.