亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過segue傳遞數據

通過segue傳遞數據

冉冉說 2019-08-02 16:05:29
通過segue傳遞數據我正在使用tableview控制器和detailView進行簡單的iOS應用程序。我想要的只是通過segue傳遞數據。這就是它的樣子。我只想點擊“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")]
查看完整描述

3 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

Swift的工作方式與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)}

如果您需要任何其他信息,請告訴我


查看完整回答
反對 回復 2019-08-02
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

使用Swift 3和4

在第一個ViewController中(發送值)

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

在第二個viewController(Catch The Value)中

var verificationId = String()


查看完整回答
反對 回復 2019-08-02
?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

如果您不需要通過標識符識別操作,只能通過目標類識別...

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


查看完整回答
反對 回復 2019-08-02
  • 3 回答
  • 0 關注
  • 598 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號