【问题标题】:Pass data through segue通过 segue 传递数据
【发布时间】:2014-11-30 05:13:42
【问题描述】:

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

这就是它的样子。

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

我的表格视图单元格:

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

【问题讨论】:

标签: ios iphone uitableview swift detailview


【解决方案1】:

使用 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()

【讨论】:

  • var var_name = String() - 在第二个视图控制器中使用它来捕获值
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 萨沙。检查我的答案。我需要喜欢。如果将在 CELL1 上单击它将打开链接 1,如果将在 CELL2 上单击它将打开链接2
    • 我尝试了 2) 方法,但我总是得到一个异常,告诉我“Receiver PlayerTableViewController 没有标识符为'playerSegue'的segue”。只需调用performSegueWithIdentifier 就可以正常工作,但是没有数据传输到新视图。 :[
    • @althaus @SashaReid 不是更好地使用indexPathForSelectedRow 而不是didSelectRowAtIndexPath 回调吗?示例:destinationVC.programVar = products[self.tableView.indexPathForSelectedRow!.row]prepareForSegue 方法中。如果我错了,请告诉我(我也是新手)。
    • @SashaReid 我遇到了与之前评论中提到的 althuas 相同的问题。我正在尝试使用第二种方法并且有点困惑。在示例中,segue 是从 HomeTableVC 到 PlayerTableVC,那么为什么我们使用 destinationVC.performSegueWithIdentifier("playerSegue", sender: self) ?当前的 VC 不是在执行 segue 吗?
    • 我认为最后一行调用 .performSegueWithIdentifier() 应该在 self 而不是 destinationVc 上调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2019-05-11
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多