简体   繁体   中英

Passing data while dismissing a view

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?

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

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 .


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 .


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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM