通过segue传递数据

pat*_*lis 66 iphone uitableview ios detailview swift

我正在使用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")]
Run Code Online (Sandbox Code Playgroud)

小智 129

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

HomeTableViewController(这是你上面的截图)

PlayerTableViewController(这是你想去的玩家屏幕)

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

class PlayerTableViewController: UITableViewController {

    var programVar : Program?

    //the rest of the class methods....
Run Code Online (Sandbox Code Playgroud)

之后,有两种方法可以将数据发送到新的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
    }
}
Run Code Online (Sandbox Code Playgroud)

一旦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)
}
Run Code Online (Sandbox Code Playgroud)

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

  • @SashaReid我遇到了与之前评论中提到的althuas相同的问题.我正在尝试做第二种方法而且有点困惑.在示例中,segue从HomeTableVC转到PlayerTableVC,为什么我们使用destinationVC.performSegueWithIdentifier("playerSegue",sender:self)?当前的VC不是正在执行segue的那个吗? (28认同)
  • 我尝试了2)方法,我总是得到一个异常,告诉"Receiver PlayerTableViewController没有带标识符'playerSegue'的segue".只是调用`performSegueWithIdentifier`工作正常,但没有数据传输到新视图.:[ (5认同)
  • @althaus @SashaReid不是更好用`indexPathForSelectedRow`而不是`didSelectRowAtIndexPath`回调?示例:`prepareForSegue`方法中的`destinationVC.programVar = products [self.tableView.indexPathForSelectedRow!.row]`.如果我错了,请告诉我(我也是新手). (2认同)
  • 我认为调用 .performSegueWithIdentifier() 的最后一行应该在 self 而不是 destinationVc 上调用 (2认同)

Imt*_*tee 41

使用Swift 3和4

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


And*_*nza 6

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

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