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

8
  • println? isn't that Java? Commented May 7, 2015 at 14:54
  • possible duplicate of Passing Data between View Controllers Commented May 7, 2015 at 15:08
  • @gutenmorgenuhu I tried but i am getting following error fatal error: unexpectedly found nil while unwrapping an Optional value How can I resolve ? Commented May 7, 2015 at 15:10
  • to answer that, we need more code: where does the error occur, when does it occur. etc Commented May 7, 2015 at 15:11
  • @Okan Did you follow the link I commented? Commented May 7, 2015 at 15:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I am not using segues for this operation. I couldn't understand you answer.
segues are the way to go to pass on data between view controllers. I have edited my answer for your question.
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 ?
@RuudKalis, "are the way to go." Only IF you are using segues. (which I think the OP is using).
Agree @Firo.. looking at the Okan's screenshot of the IB builder there is a push segue used to open the SettingsTableViewController.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.