Swift pass data through navigation controller

后端 未结 8 2115
青春惊慌失措
青春惊慌失措 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条回答
  • 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
    }
    
    0 讨论(0)
  • 2020-12-12 18:45

    FIXED syntax for SWIFT 3

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let navVC = segue.destination as! UINavigationController
            let tableVC = navVC.viewControllers.first as! YourTableViewControllerClass
    
            tableVC.yourTableViewArray = localArrayValue
        }
    
    0 讨论(0)
提交回复
热议问题