问题描述
我需要更新 TableViewController 中的一个变量,但我找不到这样做的方法,有人可以解释一下为什么这不起作用吗?我要生气了.
I need to update a variable in a TableViewController and I can't find the way to do it, can someone explain me why this is not working please?I'm getting mad.
从我的视图控制器这是我正在运行的代码:
From my view controller this is the code I'm running:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let TV = storyboard.instantiateViewControllerWithIdentifier("tbController") as myTableViewController
TV.x = "test"
然后,来自 TableViewController 类:
And then, from the TableViewController class:
class myTableViewController: UITableViewController {
var x:String!
override func viewDidLoad() {
super.viewDidLoad()
println("Value of x is: \(self.x)")
}
}
打印出来的值为:nil
And the printed value is: nil
为什么?这有什么问题?我不明白:-(
Why? What is wrong with that? I don't understand :-(
更新图片
推荐答案
首先,给 ViewController 和 TableViewController 之间的 segue 一个标识符(例如:TableViewSegue"
First, give the segue between the ViewController and the TableViewController an identifier (Example: "TableViewSegue"
然后在ViewController中,使用prepareForSegue
将数据从ViewController传递到TableViewController
Then in the ViewController, use prepareForSegue
to pass data from ViewController to TableViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "TableViewSegue" {
let vc = segue.destinationViewController as myTableViewController
vc.x = "Test"
}
}
这篇关于更新 TableViewController 中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!