ios - 将数据传递给 TableView Controller

标签 ios uitableview swift

我有一个 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:

enter image description here

最佳答案

将 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,如下所示: Push segue identifier in IB

编辑:最初稍微误解了这个问题,因为数据不是从 SettingsTableViewController 传递到 settingsViewController,而是相反。使用 segues 和 prepareForSegue 通常是将数据传递到另一个 viewController 的方法。

edit2:更改了在 prepareForSegue 函数中设置开关的方式。

关于ios - 将数据传递给 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104533/

相关文章:

iphone - 如何将委托(delegate)分配给第二个 View Controller

ios - 请求访问通讯簿时出现MachMessagePort错误

ios - 如何让 TableView 中的 SearchBar 看起来像设置应用程序中的搜索栏

properties - Swift 属性覆盖不起作用

ios - (quickblox) 如何将人员添加到聊天对话框?

iphone - 在 NSArray 中对重复项进行分组

ios - 发现意外的 Mach-O header 代码 : 0x72613c21 while generating ipa file

ios - swift 4 将具有未知根名称的 json 解码为 TableView

ios - 使用 UISearchDisplayController 时未发生选择

swift mapkit 当前位置