通过segue传递数据 [英] Pass data through segue

查看:74
本文介绍了通过segue传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它的外观如何。

我想要的只是你点击在Markíza上,它将打开URL视频编号1,如果你点击TV JOJ,它将在播放器中打开URL视频编号2.

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.

我的tableview单元格:

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


推荐答案

Swift的工作方式与Obj-C完全相同,但在新语言中重新编写。我的帖子中没有很多信息,但是让我们为每个TableViewController命名,以帮助我解释。

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.

HomeTableViewController (这是您上面的截图)

HomeTableViewController (this is the screenshot you have above)

PlayerTableViewController (这是你要前往的玩家屏幕)

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:

class PlayerTableViewController: UITableViewController {

    var programVar : Program?

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

之后有两种方法可以将数据发送到新的TableViewController。

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

1)使用prepareForSegue

在HomeTableViewController的底部,您将使用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:

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加载,变量将被设置并可用

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

2 )使用didSelectRowAtIndexPath

如果需要根据选择的单元格发送特定数据,则可以使用didSelectRowAtIndexPath。为此,您需要在故事板视图中为您的segue命名(如果您需要知道如何执行此操作,请告诉我。)

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).

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

这篇关于通过segue传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆