Instantiating view controllers


#1

In the introduction, you say that you always create view controllers programmatically, and that instantiating view controllers in Xib files leads to projects that are difficult to comprehend and debug.

What, specifically, makes this problematic? And which difficulties would you encounter?

Are there any specific examples, and how does creating the view controllers in code avoid these problems?

Ingve


#2

When a low memory warning occurs, a view controller’s view is trashed to free up memory. When that view controller’s view needs to go back on the screen, it reloads its XIB file.

If there are controller objects (or really, any non-view objects) in a XIB file, those also get recreated. Not knowing this can put your application into an inconsistent state, because a controller that may have held state (or held references to model objects that held state) will be destroyed.

It could be argued that everyone should know this and take the appropriate action to work around this. However, there are a few problems with that argument: not everyone knows this and even if they do know this, they are forced to open up XIB files to see the controller hierarchy AND read through code. This makes things difficult to maintain.