SWIFT的工作方式与OBJ-C完全相同,但在新语言中进行了修改.我从您的帖子中没有太多信息,但让我们 for each TableViewController命名,以帮助我进行解释.
HomeTableViewController(这是上面的屏幕截图)
PlayerTableViewController(这是您想要访问的播放器屏幕)
也就是说,在PlayerTableViewController中,您需要有一个变量来存储传递的数据.在您的类声明下面有类似这样的内容(如果您打算将 struct 存储为单个对象,而不是数组:
class PlayerTableViewController: UITableViewController {
var programVar : Program?
//the rest of the class methods....
之后,您可以通过两种方式将数据发送到新的TableViewController.
1) Using prepareForSegue个
在HomeTableViewController的底部,您将使用prepaareForSegue方法传递数据.以下是您将使用的代码示例:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
// Create a variable that you want to send
var newProgramVar = Program(category: "Some", name: "Text")
// Create a new variable to store the instance of PlayerTableViewController
let destinationVC = segue.destinationViewController as PlayerTableViewController
destinationVC.programVar = newProgramVar
}
}
加载PlayerTableViewController后,变量即已设置并可用
2) Using didSelectRowAtIndexPath
如果需要根据 Select 的单元格发送特定数据,可以使用didSelectRowAtIndexPath.为了让这项功能发挥作用,你需要在故事板视图中为你的segue命名(如果你也需要知道怎么做,请告诉我).
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Create a variable that you want to send based on the destination view controller
// You can get a reference to the data by using indexPath shown below
let selectedProgram = programy[indexPath.row]
// Create an instance of PlayerTableViewController and pass the variable
let destinationVC = PlayerTableViewController()
destinationVC.programVar = selectedProgram
// Let's assume that the segue name is called playerSegue
// This will perform the segue and pre-load the variable for you to use
destinationVC.performSegueWithIdentifier("playerSegue", sender: self)
}
如果您需要关于这个的任何其他信息,请告诉我