【问题标题】:swift navigation controller return nil when trying to navigate to another View Controller尝试导航到另一个视图控制器时,快速导航控制器返回 nil
【发布时间】:2020-01-06 11:34:47
【问题描述】:

我们有 Router 类将 viewController 导航到另一个视图控制器,它按预期工作,但是当 viewControllersStack 随机变为 nil 并发生崩溃时。我们尝试使用“if let”来避免崩溃,但这里的问题是当 viewControllersStack 为 nil 时出现黑屏。所以我们把它恢复了。如果导航为 nil,您能否建议为什么导航堆栈为 nil 如何处理?

private func PopOrPushToViewController(_ strControllerClass: String) {

    //get the list of controllers in the stack
    let viewControllersStack: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
    var boolDidNaviagtion = false
    for viewController in viewControllersStack {
        if boolDidNaviagtion {
            viewController.removeFromParent()
        }
        if String(describing: type(of: viewController)) == strControllerClass {
            boolDidNaviagtion = true
            self.navigationController?.popToViewController(viewController, animated: true)
        }
    }
    if !boolDidNaviagtion {
        let viewController = NavigationController.sharedInstance.storyboardViewControllerFromString(strControllerClass)!
        self.navigationController!.pushViewController(viewController, animated: true)
    }
}



class AddTripViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func navigate(_ sender: Any) {
        popOrPushToViewController( "ListViewController")
    }

}

【问题讨论】:

    标签: ios swift xcode navigation pushviewcontroller


    【解决方案1】:

    问题很可能是您使用的:

    viewController.removeFromParent()
    

    如果您pop 到堆栈中的一个 VC,其他 VC 将被自动删除。

    尝试将您的功能更改为:

    private func PopOrPushToViewController(_ strControllerClass: String) {
    
        // get the list of controllers in the stack
        if let vcStack: [UIViewController] = self.navigationController?.viewControllers {
    
            var didPop = false
    
            for vc in vcStack {
    
                // if we find the desired VC already in the navVC's stack of controllers,
                // pop to it, set the didPop flag to true, and quit looking
                if String(describing: type(of: vc)) == strControllerClass {
                    self.navigationController?.popToViewController(vc, animated: true)
                    didPop = true
                    break
                }
    
            }
    
            // if we did NOT pop to the desired VC,
            // instantiate it and push it onto the stack
            if !didPop {
                if let vc = NavigationController.sharedInstance.storyboardViewControllerFromString(strControllerClass) {
                    navigationController?.pushViewController(vc, animated: true)
                }
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多