If a dealloc on a viewcontroller is called, then the next time when the viewcontroller is "invoked" would the initWithNibName be called again?
2 Answers
Yes, but you may be misunderstanding the relationship. dealloc
means that the object is going away (not just leaving the screen). init...
means the object is being created (not just being put on the screen). The init...
after the dealloc
is sent to a completely different (newly created) object.
Many thing that people put into initWithNibName:
or dealloc
they actually mean to put into viewWillAppear:
and viewDidDisappear:
. Putting logic here ensures that it is called even if the same view controller is reused.

- 286,113
- 34
- 456
- 610
init methods are called by the software that you write, or invoked indirectly through calls to [super init...] (which is also initiated by your software).
If your view controller has it's dealloc method called, it is because it has been released to where the retain count is zero.
I'm wondering if you are more interested in the unloading and loading of the views, instead. (UPDATE) If that's the case, have a look at this thread. It explains some useful things about the loadView and viewDidLoad. More importantly, all of your view setup should be put into the viewDidLoad method, and all subview should be retained only by the main view (when you use addSubview). If you do that, then you may release the main view (in didReceiveMemoryWarning is the most likely place). After that, any reference to that view (through self.view) will reload the views and its subviews. However, in this process, the view controller is not released or deallocated.
UPDATE #2 You can use a process called lazy loading to automatically allocate and initialize your view controller. In this method, you make the view controller instance a property of the object that owns and uses it. You write your own getter method for that view controller, and in that getter, you test to see if the view controller already has been instantiated. If it has not, you instantiate it in the getter. Then return the instance from the getter. When you do this, make sure that you set the instance to nil when you release it. (It is probably a good idea to make sure it is not owned/retianed somewhere else when you are doing this.
Here is an example of such a getter:
- (MyVC*) myVC {
if (!myVC) {
myVC = [[MyVC alloc] initWithNib...];
// include other things here that you might need for setting the VC up
}
return myVC;
}
Remember to set the value to nil when you release it.