ios 通过segue传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26207846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:01:57  来源:igfitidea点击:

Pass data through segue

iosiphoneuitableviewswiftdetailview

提问by patrikbelis

I'm doing simple iOS application with tableview controller and detailView. All I want is to pass data through segue.

我正在使用 tableview 控制器和 detailView 做简单的 iOS 应用程序。我想要的只是通过 segue 传递数据。

this is how it looks like

这是它的样子

this is how it looks.

这是它的样子。

All I want is that u click on "Markíza" it will open URL video number 1 and if u click on "TV JOJ" it will open URL video number 2 in player.

我想要的只是你点击“Markíza”它会打开 URL 视频 1,如果你点击“TV JOJ”它会在播放器中打开 URL 视频 2。

My tableview cells:

我的表格单元格:

    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")]

回答by Sasha Reid

Swift works exactly the same way as Obj-C but is reworked in the new language. I don't have a lot of information from your post but let's give a name to each TableViewController to help with my explanation.

Swift 的工作方式与 Obj-C 完全相同,但在新语言中进行了重新设计。我没有从您的帖子中获得很多信息,但让我们为每个 TableViewController 命名以帮助我进行解释。

HomeTableViewController(this is the screenshot you have above)

HomeTableViewController(这是你上面的截图)

PlayerTableViewController(this is the player screen you want to travel to)

PlayerTableViewController(这是您要前往的播放器屏幕)

With that said, in PlayerTableViewController you need to have a variable that will store the passed data. Just under your class declaration have something like this (if you intend to store the struct as a single object rather than the array:

话虽如此,在 PlayerTableViewController 中,您需要有一个变量来存储传递的数据。就在你的类声明下面有这样的东西(如果你打算将结构存储为单个对象而不是数组:

class PlayerTableViewController: UITableViewController {

    var programVar : Program?

    //the rest of the class methods....

After that there are two ways you can send data to the new TableViewController.

之后,您可以通过两种方式将数据发送到新的 TableViewController。

1) Using prepareForSegue

1) 使用 prepareForSegue

At the bottom of HomeTableViewController you will use the prepareForSegue methods to pass the data. Here is an example of the code you'll use:

在 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
    }
}

Once PlayerTableViewController has loaded the variable will be already set and usable

一旦 PlayerTableViewController 加载了变量,就已经设置好并且可以使用了

2) Using didSelectRowAtIndexPath

2) 使用 didSelectRowAtIndexPath

If specific data needs to be sent based on which cell is selected you can use didSelectRowAtIndexPath. For this to work, you need to give your segue a name in the storyboard view (let me know if you need to know how to do this too).

如果需要根据选择的单元格发送特定数据,您可以使用 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)
}

Let me know if you need any other info on this

如果您需要任何其他信息,请告诉我

回答by Imtee

With Swift 3 & 4

使用 Swift 3 & 4

In The First ViewController (Send The Value)

在第一个 ViewController 中(发送值)

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

In the second viewController (Catch The Value)

在第二个 viewController 中(Catch The Value)

var verificationId = String()

回答by Andrea Leganza

If you have no need to discern the action by the identifier but only by the target class...

如果您不需要通过标识符来辨别动作,而只需通过目标类...

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