Swift pass data through navigation controller

后端 未结 8 2118
青春惊慌失措
青春惊慌失措 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:24

    Override prepareForSegue and set whatever value on the tableview you'd like. You can grab the tableview from the UINavigation viewControllers property.

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!){
    
       let navVC = segue.destinationViewController as UINavigationController
    
       let tableVC = navVC.viewControllers.first as YourTableViewControllerClass
    
       tableVC.yourTableViewArray = localArrayValue   
    }
    

    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   
    }
    

提交回复
热议问题