3 回答
TA贡献1815条经验 获得超6个赞
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)}如果您需要任何其他信息,请告诉我
TA贡献1803条经验 获得超6个赞
使用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()
TA贡献1998条经验 获得超6个赞
如果您不需要通过标识符识别操作,只能通过目标类识别...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? YourViewController {
vc.var_name = "Your Data"
}}- 3 回答
- 0 关注
- 616 浏览
添加回答
举报
