Swift通过导航控制器传递数据 [英] Swift pass data through navigation controller

查看:96
本文介绍了Swift通过导航控制器传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个segue从视图控制器转换到tableview控制器。我想在两者之间传递数组,但是在tableview控制器之前使用导航控制器,我无法弄清楚如何传递数组。如何通过navigatiom控制器将数据传递给tableview控制器?

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?

推荐答案

覆盖 prepareForSegue 并设置您想要的tableview上的任何值。您可以从 UINavigation viewControllers 属性中获取tableview。

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   
}

对于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   
}

这篇关于Swift通过导航控制器传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆