2

I'm developing a Mac Application. The application has a common source view on the left and a detail view on the right which is the main part of the whole window.

It's like a Master-Detail relationship, but each element in the source view require another detail view. In fact, I have designed a specific NSViewController for each element in the source view.

If I'm switching between these NSViewControllers, that means If I select another element in the source view, I remove the current view and add the view of the newly selected NSViewController. Everytime I change the NSViewController, its state will be lost. When the user comes back to that NSViewController, he has to start over.

My question now is: How can I save the state of the NSViewController, so that I can switch between these without losing its states and can continue where I have left?

burki
  • 2,946
  • 6
  • 37
  • 51

2 Answers2

0

Use NSArchiver. Implement archiving/unarchiving in your dealloc/init methods and store each view controller's state in a file named after the class (if you have one item per view controller policy). Otherwise think of some simple naming convention and use it.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
0

Two considerations about your problem:

  • Keep model data in model classes. This means that you can always recreate a view controller and set its represented object provided the model classes have kept the changes made via the view controller. When you need to instantiate a view controller, set its represented object to (a representation of) a model class.

  • When removing a view from its superview, you do not necessarily need to release its corresponding view controller. Instead, you can keep strong references to all view controllers in your window controller/application delegate, so no state is actually lost.

  • Thanks for the answer! I've already thought of making a reference between the view controllers and the source list elements. But won't cause this bad performance and waste of memory? – burki Jun 03 '11 at 17:39
  • @burki It depends on the design and the amount of data in your application, so it’s hard to tell without knowing the details of your application. –  Jun 03 '11 at 20:24