ios - 更新 TableViewController 中的变量

标签 ios uitableview swift parameter-passing

我需要更新 TableViewController 中的一个变量,但我找不到方法,有人可以解释一下为什么这不起作用吗? 我要生气了。

enter image description here

在我的 View Controller 中,这是我正在运行的代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let TV = storyboard.instantiateViewControllerWithIdentifier("tbController") as myTableViewController
TV.x = "test"

然后,从 TableViewController 类:

class myTableViewController: UITableViewController {
 var x:String!
 override func viewDidLoad() {
     super.viewDidLoad()
     println("Value of x is: \(self.x)")
 }
}

打印出来的值是:nil

为什么?这有什么问题?我不明白:-(

更新图片 enter image description here

最佳答案

首先,给 ViewController 和 TableViewController 之间的 segue 一个标识符(例如:“TableViewSegue”

然后在 ViewController 中,使用 prepareForSegue 将数据从 ViewController 传递到 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"
    }
}

关于ios - 更新 TableViewController 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533449/

相关文章:

iphone - 比较同一 UIViewController 类的实例或对象

objective-c - 我可以将委托(delegate)作为参数传递吗?

iphone - 如何通过我的应用程序为所有不同的 UITableView 提供相同的外观?

iphone - 如何判断 iPhone 应用程序返回前台后当前的 UIView/UIController 是什么?

ios - 自定义View ui元素显示

ios - SpriteKit : Why containsPoint of SKNode returns false

ios - 应用程序内的日历 View

iOS 7 : UITableViewController: Changing/Replacing Delete Button

ios - 所有章节标题显示在一个章节中

string - 如何快速创建包含大写字母的单词列表?