我正在用TableView控制器和Detail View做简单的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")]

推荐答案

SWIFT的工作方式与OBJ-C完全相同,但在新语言中进行了修改.我从您的帖子中没有太多信息,但让我们 for each TableViewController命名,以帮助我进行解释.

HomeTableViewController(这是上面的屏幕截图)

PlayerTableViewController(这是您想要访问的播放器屏幕)

也就是说,在PlayerTableViewController中,您需要有一个变量来存储传递的数据.在您的类声明下面有类似这样的内容(如果您打算将 struct 存储为单个对象,而不是数组:

class PlayerTableViewController: UITableViewController {

    var programVar : Program?

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

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

1) Using prepareForSegue

在HomeTableViewController的底部,您将使用prepaareForSegue方法传递数据.以下是您将使用的代码示例:

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) Using didSelectRowAtIndexPath

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

如果您需要关于这个的任何其他信息,请告诉我

Ios相关问答推荐

Flutter应用程序无法使用IOS/Swift的蓝牙核心库发现某些外围设备

阿拉伯文变音符号(Harakat)在文本对齐的UILabel中未对齐

如何解释在SwiftUI视图中观察到持续的数据更改

UITableViewCell编程约束问题

当操作修改查看条件时,按钮不重复

确保方法调配的安全有效实施

WatchConnectivity - 从手表配套应用程序在 iOS 设备上启动 AVPlayer

使用 Xamarin 将 dext Bundle 到 iOS .ipa 中

尽管存在浮点错误,如何显示正确的计算结果.计算器应用程序

xcode-select:找不到clang

弹出视图后使所有背景 UI 模糊

在向我的 struct 添加属性后获取线程 10:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

在 Swift 中合并子数组

如何在不同设备的segment控制中管理文本大小(text size)?

刷新由 Xcode 7 管理的团队配置文件中的设备?

在 iOS 8.1 模拟器上更改语言不起作用

文本未包含在 swift UI 中

如何使用 AFNetworking 设置超时

Twitter api 文本字段值被截断

类 AMSupportURLConnectionDelegate 在两者中都实现