I'm writing an iPhone app, and upon a certain event, say, the user winning the game, I would like to reset the application back to its initial state right after it is launched. For example, executing viewDidLoad() again, etc. Is there a simple way to do this in Xcode and objective C? Thank you.
-
3This really depends on how your app is structured. – PengOne Jun 28 '11 at 22:11
1 Answers
You're in charge of the state of your app -- if you know what the initial settings are, you should be able to set everything back to that state. There's no simple [UIApplication reset]
method, but there are a few techniques that make the job easier:
Post a notification when you want to reset the app, and make sure that any objects that might need to reset themselves (such as view controllers) listen for that notification.
Move your initialization code into a different method, and then call it from both your
-init
method and your reset notification handler.You can get a view controller to reload its view by setting its view property to nil. The next time that property is accessed,
-loadView
will be called, and that in turn will load the relevant .xib or create the necessary views.In many cases, you may be able to just reset the app's data model to an initial state. The views in your app should typically reflect the state of the data model, so clearing the data should cause the views to go back to their initial state as well.
Consider why you really need this. It might make sense if you have a "reset to factory defaults" button somewhere, but it might also be an indication that more design is required. Why can't the user just select all the data and delete it? What state are you holding onto that the user might want to reset in this way?

- 124,013
- 19
- 183
- 272
-
Or you can just reload the root view controller, if there's no extraneous state floating around. (Ewwww.) – tc. Jun 28 '11 at 22:17
-