斯威夫特作品完全方式的OBJ-C相同,但在新的語言重寫。我的帖子中沒有很多信息,但讓我們給每個TableViewController一個名字,以幫助我解釋。
HomeTableViewController(這是你有上面的截圖)
PlayerTableViewController(這是您要前往的播放器屏幕)
隨着中說,在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)
}
讓我知道,如果你需要對這個
,但我認爲這是Objective-C和我需要它的迅速。 – patrikbelis 2014-10-05 22:43:44
[將變量從一個ViewController傳遞給另一個Swift中的可能副本](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – Paulw11 2014-10-05 23:05:36