Swift pass data through navigation controller

后端 未结 8 2127
青春惊慌失措
青春惊慌失措 2020-12-12 18:13

One of my segues transitions from a view controller to a tableview controller. I want to pass an array between the two, but with a navigation controller before the tableview

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 18:35

    I was getting this error to Horatio's solution.

    ERROR: Value of type 'UINavigationController' has no member 'yourTableViewArray'

    So this might help others like me looking for a code only solution. I wanted to redirect to a Navigation controller, yet pass data to root view controller within that Nav Controller.

    if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigationControllerID") as? UINavigationController,
       let yourViewController = controller.viewControllers.first as? YourViewController {
            yourViewController.yourVariableName = "value"
            self.window?.rootViewController = controller  // if presented from AppDelegate
            // present(controller, animated: true, completion: nil) // if presented from ViewController
    }
    

提交回复
热议问题