【问题标题】:Instantiate a vc keeping the navigation bar Swift实例化一个保持导航栏Swift的vc
【发布时间】:2020-01-05 10:31:27
【问题描述】:

我想在响应远程通知操作时显示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】:

    看起来windows rootViewController 已经是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)
         }
    

    【讨论】:

    • 谢谢。我仍然会在我的更新代码中显示启动画面,但我只是希望应用程序在NewMapViewController 上启动,点击操作按钮。有没有办法避免显示?
    • 我认为您无能为力,因为操作系统会在恢复应用程序时显示启动画面(或操作系统捕获的最新屏幕截图)。
    • 我明白了,所以我只能坚持下去。非常感谢您的帮助。
    • @Karman 我刚刚发现这种到达NewMapViewController 的方式会导致函数出现奇怪的行为。如果NewMapViewController.routeCheckCounter' is greater than 0. It loads a route, displays it as a MKPolyline` 会调用此函数并调整视图大小以放大它。由于问题的变化,它不再设置区域。你知道为什么吗?我的意思是该功能的其余部分正确地完成了它的工作。
    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2013-05-07
    • 2017-08-13
    • 2018-10-28
    • 2016-04-18
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多