A detached table delegate must be referred in the NIB

Here’s a puzzling problem I just encountered a third time, and wasted a better part of the morning sorting out, which I had solved before but forgot to record my solution.

On the iPhone, I wanted to create an object which serves as a delegate to a UITableView. This object is then instantiated in the NIB file alongside the UIViewController which owns the control on which the UITableView lives. So I created an instance, and pointed the delegate and dataSource references of the UITableView at my table controller object.

On startup, my iPhone application crashed.

The solution appears to be creating a reference to my object from the owner of the NIB.

My mental model as to how a NIB is loaded on the phone is that every object is instantiated and initialized, then events are fired to begin populating things like the contents of the table. In other words, objects are loaded then events fired. What appears to be going on, however, is that objects are loaded then initialized in an order-dependent fashion which I don’t completely understand–however, by creating a reference from the owner to the delegate object (which is after the table item), the delegate object and the table object are loaded and initialized in the correct order.

So the lesson appears to have the owner object hold references to both the table (indirectly via the view containing the table) and the delegate object which controls the table.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s