Search code examples
iosobjective-cuiviewcontrolleruistoryboarduistoryboardsegue

dismissViewController but keep reference


Lets say I have an app which basically exist of a tabbarcontroller with views. One of the views has a button which presents a modal viewcontroller. The purpose of this viewcontroller is to enter some data like a name and when pressing on start it starts recording so it also keeps a reference to the time and displays the total amount of recording time.

(After clicking the start button the button changes to a stop button)

During recording the user can click the close button which will dismiss the viewcontroller (holding the variables like name and recording time) and the app returns to its tabbarcontroller with views. When the user presses the button (which presents the modal viewcontroller) again while recording it must show the viewcontroller with the data entered previously (name, total amount of recording time).

So this data must be kept alive after the following code runs:

- (IBAction)close:(id)sender {
     [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}

The code above will dismiss the viewcontroller but also destroys the variables in that viewcontroller. What would be the best way to keep the variables entered in that viewcontroller after dismissing it?

Should the tabbarcontroller's view be responsible for this data? Or should I keep a reference to my modal viewcontroller and keep it alive in the background? And how would I do this in code?

Finally when the user does stop the recording by clicking on the stop button in the modal view controller and presses the close button this view should be destroyed to preserve memory of course.


Solution

  • If you want to store whole instance of ViewController you should create an Singleton class with deallocation aborting method:

    -(void)dealloc {
       abort();
    }
    

    But i think you want to store only a few information from it. You could pass it using viewWillDissapear method ant there called topViewController (in UINavigationController based app) and assign some variables to destination.

    Also you could use singleton data model and assign values to this when you dismiss controller.

    What i suggest you to do is strongly separate dataModel from ViewController to separate class.