简体   繁体   中英

What is the best way to keep the state of a modal UIViewController?

I'm having a hard time to sum up my problem in one title so I'll try to be clear here.

I have a UIViewController (vc1) that links me to another UIViewController (vc2) through a segue.

In vc2 I can select a segment of a UISegmentedControl .

To go back to vc1, I simply press a return UIButton I made where the @IBAction is:

@IBAction func returnToMainView(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

I would like that when I go back again to vc2, the segment I selected the first time is again selected.

What are my solutions?

Should I keep using the dismissViewControllerAnimated method and use a delegate method to save which index segment is selected. Then when I go back to vc2, pass the selected index segment in the segue calling for vc2?

Or is there a better way to keep the state of my vc2 than the dismissViewControllerAnimated ?

Hope I've been clear enough.

UPDATE :

Here is some more information on my case.

In vc1, I have a UITableView. On every row I can call my vc2 which will display information according to the row of course.

And actually, I want to save the state of this controller just for the current execution of the app. No need to persist the state

I'm using NSUserDefaults for that. So If I open another ViewController, I save all important settings of my UIViews where needed.

In your example, before you dismiss your ModalViewController, you can save for example the state of your UISegmentedControl :

NSUserDefaults.standardUserDefaults().setInteger(segmentedControl.selectedSegmentIndex, forKey: "yourSwitch")

Then, in the viewWillApear method of your ModalViewController, you call the NSUserDefaults and load all settings:

var selectedIndex = NSUserDefaults.standardUserDefaults().integerForKey("yourSwitch")

After that in the viewDidAppear method, you call the values:

segmendedControl.selectedSegmentIndex = selectedIndex

IMO if it is really just one segmented_control index you want to retain, save its index in your previous viewController and pass it anytime you present it via the prepareForSegue: method. Any other thing would be an overkill.

There are many ways you can do this:

  1. Delegate pattern. You can have VC1 implement VC2Delegate protocol which essentially acts as a callback
  2. You can set a block property on VC2 from VC1 to perform some action to change the state of VC1 when the segmentedControl is augmented.
  3. As some of the other answers mentioned, you could set some data model like Core Data or NSUserDefaults .
  4. You could use NSNotifications. In my experience this can be tough for other engineers to understand what is happening.

As others have pointed out, there are several ways to do this. Another way is to keep VC2 alive, rather than having it be deallocated when you dismiss it. You can do this by creating a property for it in VC1, and only instantiating VC2 the first time you present it; you will have to present it in code, rather than using a segue, since segues always instantiate new controllers.

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