Swift pass data through navigation controller Swift pass data through navigation controller ios ios

Swift pass data through navigation controller


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   }


@Tommy Devoy's answer is correct but here is the same thing in swift 3

Updated Swift 3

  // MARK: - Navigation//In a storyboard-based application, you will often want to do a little preparation before navigationoverride func prepare(for segue: UIStoryboardSegue, sender: Any?) {    // Get the new view controller using segue.destinationViewController.    // Pass the selected object to the new view controller.    if segue.identifier == "yourSegueIdentifier"  {        if let navController = segue.destination as? UINavigationController {            if let chidVC = navController.topViewController as? YourViewController {                 //TODO: access here chid VC  like childVC.yourTableViewArray = localArrayValue             }        }    }}


SWIFT 3 (tested solution) - Passing data between VC - Segue with NavigationController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {        let navigationContoller = segue.destination as! UINavigationController        let receiverViewController = navigationContoller?.topViewController as ReceiverViewController        receiverViewController.reveivedObject = sentObject     }