unixo's blog

NSViewController (Bad) Initialization

Since 2008 NSViewController are considered an incomplete implementation, most of all if compared to
its counterpart NSWindowController (for example NSViewController doesn’t ensure that its view is
added into the responder chain).

Some days ago, I encountered another issue about view controllers which completely drove me crazy; in
my application I’ve a left sidebar and a right subview based on selection of the first one: each time
the user makes a selection, a new subview is loaded by an equivalent NSViewController and presented to
the user.

As well as with NSWindowController I put some basic initialization in awakeFromNib method of NSViewController,
to be sure that NIB was loaded and all IBOutlet connected. In particular, in a subview, I had something like this:

Occasionally I noticed that reloadItems was called twice (or more), even much time later after initialization.
After a boring debug session, I realized that in that subview I also had a view-based NSTableview and the view
controller was its datasource and delegate.