1

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:

enter image description here

Okan
  • 1,379
  • 3
  • 25
  • 38

1 Answers1

1

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: Push segue identifier in IB

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.

Ruud Kalis
  • 264
  • 3
  • 9
  • I am not using segues for this operation. I couldn't understand you answer. – Okan May 07 '15 at 15:23
  • segues are the way to go to pass on data between view controllers. I have edited my answer for your question. – Ruud Kalis May 07 '15 at 15:30
  • I get it you now. But I have one more problem. I am getting data from server so I need to wait data then when it comes I need to use in segue. How can I do this ? – Okan May 07 '15 at 15:34
  • @RuudKalis, "are the way to go." Only IF you are using segues. (which I think the OP is using). – Firo May 07 '15 at 15:34
  • Agree @Firo.. looking at the Okan's screenshot of the IB builder there is a push segue used to open the SettingsTableViewController. – Ruud Kalis May 07 '15 at 15:36
  • @Okan: the problem with data arriving from the server is a different question. Please search on StackOverflow and if you do not find an answer, post a new question. – Ruud Kalis May 07 '15 at 15:37
  • Can I use notification center for this case ? – Okan May 07 '15 at 15:40
  • Notifications are used inside the app to send notifications to one or several listeners (e.g. viewControllers). If you have a network server sending data I would use sockets. Check this tutorial: http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server – Ruud Kalis May 07 '15 at 15:55
  • You got it wrong. After I receive the data from server can I use notification center to pass to tableview controller ? – Okan May 07 '15 at 16:07
  • I would not use the notification center for this problem. The notification center is used to display a message, play a sound or update a badge on the app's icon. They are not designed to send data to a tableview. https://developer.apple.com/notifications/ – Ruud Kalis May 07 '15 at 16:18