Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift pass data through navigation controller

Tags:

ios

swift

segue

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 controller, I can't figure out how to pass the array. How do you pass data through a navigatiom controller to a tableview controller?

like image 674
user3142972 Avatar asked Aug 18 '14 18:08

user3142972


1 Answers

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    } 
like image 92
Tommy Devoy Avatar answered Sep 20 '22 13:09

Tommy Devoy