Question: I'm working on an app with two different views which can modally present a "settings" viewController with a tableView embedded in it. To pass the data from the embedded tableView to the first two views I would have to use prepareForSegue in each view transition (to my understanding). However, when I'm in the Settings view and tap on "Done" I want to go back to the view I was before. In order to do this one would have to dismiss the view as far as I know. Can I pass data from one view to another while dismissing the view? If not, how would this be accomplished?

[![](https://i.stack.imgur.com/z1ghy.png)](https://i.stack.imgur.com/z1ghy.png)

*Note: Feel free to correct me, I'm still beginning app development and some things I said might be completely wrong.*

Answer: 
There are a few ways you can accomplish this.

You can define an **unwind segue** from your presented view controller back to another view controller further back (usually the VC that did the original presentation). Unwind segues can be triggered from buttons much like "regular" segues, and since they're segues, they trigger `-prepareForSegue:` in the dismissing VC. You can use that just like you otherwise would to push data back. Unwind segues are documented in [this tech note](https://developer.apple.com/library/content/technotes/tn2298/_index.html).

---

Alternately, you can set up **delegation** from the presented view controller to another VC. This pattern is widely used in iOS development, and involves a few steps:

* Define a protocol ("SettingsDelegate") for classes to conform to. Give it a method – something like `settingsDidChange(_:)`. Make that method take an argument with the data you want to pass back.
* Give your Settings controller a weak `delegate` property of type `SettingsDelegate?`. When first presenting that VC, set the delegate to the presenter.
* Make the presenting VC conform to the delegate protocol, and implement the method to update as you see fit.
* Have the Done button in the presented VC call `self.delegate.settingsDidChange(_:)`, passing the new data. The presenting VC will get this call and update as Settings dismisses.

The delegation pattern can be tricky to set up the first time, but gets easier as you go. It's documented [here](https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html).

---

Finally, you could use a **persistent data store** to stash settings in. UserDefaults is a good option for settings data – it lets you keep key/value pairs of information in a way that's accessible throughout your app. Read up on user defaults [here](https://developer.apple.com/reference/foundation/userdefaults).

To update when user defaults values change, you could have your Settings controller post a Notification when dismissing. Then, other VCs in your app could listen for this notification and update as needed.
You are correct about `prepareForSegue`, you can't use it here because you need to use dismiss in order to pop the Settings `viewController` off of the navigation stack. A very simple solution for this would be to define a global variable. A global variable is simply a variable that exists above the scope of any classes in your app. For example, in your settings `viewController` file, you could do this.

```
var globalVariable: String
class Settings: ViewController{
    //view controller stuff
    didSelectRowAtIndexPath{
        globalVariable = "Information"
    }
}

```

Then, when you use the dismiss method, your previous viewcontroller will be able to access that variable as `globalVariable`. It doesn't have to be a string, and you don't have to set it in `didSelectRowAtIndexpath`, you can use it however you want.