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)
如果您需要任何其他信息,请告诉我
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)
如果您不需要通过标识符来辨别动作,而只需通过目标类...
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)
归档时间: |
|
查看次数: |
86947 次 |
最近记录: |