我有一个 View Controller ,我在这个 View Controller 中有一个 TableView 。我正在从 TableView Controller 管理表。但是我需要将数据从 View Controller 发送到 TableView Controller 。我怎样才能做到这一点 ?
这是我的 View Controller :
class SettingsViewController: UIViewController {
...
}
这是我的 TableView Controller :
class SettingsTableViewController:UITableViewController {
@IBOutlet var notificationsSwitch: UISwitch!
@IBAction func notificationsClicked(sender: UISwitch) {
if notificationsSwitch.on {
println("notifications on")
}else{
println("notifications off")
}
}
}
Storyboard:
最佳答案
将 Action 转场与 prepareForSegue 函数结合使用。本文描述:
Storyboards Tutorial in Swift: Part 2
为打开 SettingsTableViewController 的推送转场添加一个标识符,并将以下函数添加到 ViewController。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "unwindToTableView" {
let destinationController = segue.destinationViewController as! SettingsTableViewController
destinationController.notificationsSwitch.setOn(true, animated:true)
}
}
假设你想将一些 bool 值从 settingsViewController 传递到表中,你会得到类似这样的东西:
var notifications: Bool = false
在 Interface builder 中,将标识符添加到 push segue,如下所示:
编辑:最初稍微误解了这个问题,因为数据不是从 SettingsTableViewController 传递到 settingsViewController,而是相反。使用 segues 和 prepareForSegue 通常是将数据传递到另一个 viewController 的方法。
edit2:更改了在 prepareForSegue 函数中设置开关的方式。
关于ios - 将数据传递给 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104533/