Reputation: 1389
I have a view controller, and I have one tableview inside this view controller. I am managing the table from table view controller. But I need to send data to tableview controller from view controller. How can I do this ?
This is my view controller:
class SettingsViewController: UIViewController {
...
}
And this is my table view controller:
class SettingsTableViewController:UITableViewController {
@IBOutlet var notificationsSwitch: UISwitch!
@IBAction func notificationsClicked(sender: UISwitch) {
if notificationsSwitch.on {
println("notifications on")
}else{
println("notifications off")
}
}
}
Storyboard:
Upvotes: 1
Views: 96
Reputation: 264
Use action segues in combination with a prepareForSegue function. Described in this article:
Storyboards Tutorial in Swift: Part 2
Add an identifier for your push segue that opens the SettingsTableViewController and add the following function to ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "unwindToTableView" {
let destinationController = segue.destinationViewController as! SettingsTableViewController
destinationController.notificationsSwitch.setOn(true, animated:true)
}
}
Assuming that you want to pass some bool from the settingsViewController to the table, you will have something like:
var notifications: Bool = false
In the Interface builder, add an identifier to the push segue like this:
edit: slightly misunderstood the problem initially, since the data is not passed from SettingsTableViewController to settingsViewController but the other way around. Using segues and prepareForSegue is generally the way to pass on data to another viewController.
edit2: changed the way to set the switch in the prepareForSegue function.
Upvotes: 1