Swift通过导航控制器传递数据

我的一个segues从视图控制器转换到tableview控制器。 我想在两者之间传递一个数组,但是在tableview控制器之前有一个导航控制器,我不知道如何传递数组。 如何通过navigatiom控制器将数据传递给tableview控制器?

重写prepareForSegue并设置你想要的tableview的任何值。 你可以从UINavigation viewControllers属性中获取tableview。

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

对于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的回答是正确的,但在迅速3这里是同样的事情

更新了Swift 3

  // MARK: - Navigation //In a storyboard-based application, you will often want to do a little preparation before navigation override 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 } } } } 

Tommy的解决scheme要求你在Storyboard中设置Segue。

这是一个代码唯一的解决scheme:

 func functionToPassAsAction() { var controller: UINavigationController controller = self.storyboard?.instantiateViewControllerWithIdentifier("NavigationVCIdentifierFromStoryboard") as! UINavigationController controller.yourTableViewArray = localArrayValue self.presentViewController(controller, animated: true, completion: nil) } 

Horatio的解决scheme出现了这个错误

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

所以这可能会帮助像我这样的人寻找一个唯一的解决scheme。 我想redirect到一个导航控制器,但传递数据到该导航控制器内的根视图控制器。

 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 } 

SWIFT 3的FIXED语法

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let navVC = segue.destination as! UINavigationController let tableVC = navVC.viewControllers.first as! YourTableViewControllerClass tableVC.yourTableViewArray = localArrayValue } 

SWIFT 3(testing解决scheme) – 在VC之间传递数据Segue和NavigationController

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