Three20 - Adding Title to Table View Controller after Datasource Loads

This is actually a pretty minor little detail, but it’s something I spent about half an hour getting to work, so hopefully I can save a few people some more time.

Most of the examples with Three20 are pretty static - the controller loads the same thing every time, so you can hard-code the title to the controller in an init() method.

In my case, many of the controllers actually correspond to Drupal CCK content types, so instead of displaying a generic “Information” title, I’d prefer that the TTTableViewController display the Drupal Node title as the title for the user.

I’ve already done all of the work of loading the Drupal node as JSON and parsing it on the iPhone into an NSObject (Trail). The Trail object is a child of my TTModel (TrailModel), which is then the model of my TTSectionedDataSource(TrailDataSource). To make things confusing, the model for my TTTableViewController(TrailViewController) is the datasource.

I expected to find a title property on the TTSectionedDataSource, similar to the TTPhotoSource, but there wasn’t one, so I had to dig a little deeper. I knew I had to wait until the model finished loading so I had a business object to ask for the proper title, and I’d also found earlier that there is a pretty nice system of messaging set up in Three20 already.

In this case, the model will notify the model view controller that it is finished loading and displaying, and then I can override that method ((void) didShowModel:(BOOL) firstTime)

There really isn’t much to it other than putting whatever code I want into the didShowModel() method - I grab the title by going through the datasource to the model and then to the value object, rather than by writing a series of getters that dig down into the value object.

- (void) didShowModel:(BOOL) firstTime { 
    [super didShowModel:firstTime\]; 
    TrailModel\* model = (TrailModel\*)\[self.dataSource model\]; 
    self.title = \[\[model trail\] title\];