2014-10-05 167 views
43

我正在用tableview控制器和detailView做簡單的iOS應用程序。我想要的只是通過segue傳遞數據。通過segue傳遞數據

this is how it looks like

這是它的外觀。

我只需要點擊「Markíza」就會打開URL視頻編號1,如果你點擊「TV JOJ」,它會在播放器中打開URL視頻編號2。

我的tableview細胞:

struct Program { 
     let category : String 
     let name : String 
    } 


    var programy = [Program]() 
     self.programy = [Program(category: "Slovenské", name: "Markíza"), 
         Program(category: "Slovenské", name: "TV JOJ")] 
+0

,但我認爲這是Objective-C和我需要它的迅速。 – patrikbelis 2014-10-05 22:43:44

+0

[將變量從一個ViewController傳遞給另一個Swift中的可能副本](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift) – Paulw11 2014-10-05 23:05:36

回答

107

斯威夫特作品完全方式的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) 
} 

讓我知道,如果你需要對這個

+0

sasha。檢查我的答案。我需要喜歡。如果將點擊CELL1它將打開鏈接1,如果將點擊CELL2它將打開link2 – patrikbelis 2014-10-22 20:23:47

+3

我嘗試了2)方法,並且我總是會得到一個異常,告訴「Receiver PlayerTableViewController沒有標識符'playerSegue'的延續」。只要調用'performSegueWithIdentifier'即可正常工作,但沒有數據被轉移到新視圖。 :[ – althaus 2014-11-26 11:09:14

+0

@althaus您是否確定在故事板中有一個名爲「playerSegue」的賽格?這張圖片顯示你應該尋找什麼。如果失敗了,給我一些截圖,我會檢查出來。 https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/art/SB_H_set_segue_identifier_2x.png – 2014-11-27 11:50:04

10

任何其他信息與SWIFT 3

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if (segue.identifier == "MainToTimer") { 
     let vc = segue.destination as! YourViewController 
     vc.var_name = "Your Data" 
    } 
}