Swift 的工作方式与 Obj-C 完全相同,但用新语言进行了重新设计。我没有从您的帖子中获得太多信息,但让我们为每个 TableViewController 命名以帮助我进行解释。
主表视图控制器(这是上面的屏幕截图)
播放器表视图控制器(这是您想要前往的播放器屏幕)
话虽如此,在 PlayerTableViewController 中您需要有一个变量来存储传递的数据。在类声明下有类似的内容(如果您打算将结构存储为单个对象而不是数组:
class PlayerTableViewController: UITableViewController {
var programVar : Program?
//the rest of the class methods....
之后,您可以通过两种方式将数据发送到新的 TableViewController。
1)使用prepareForSegue
在 HomeTableViewController 的底部,您将使用prepareForSegue 方法来传递数据。以下是您将使用的代码示例:
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)使用didSelectRowAtIndexPath
如果需要根据选择的单元格发送特定数据,您可以使用 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)
}
如果您需要任何其他信息,请告诉我