Today I would like to give some words about a popular and powerful open source piece of software called Three20.

Three20 (aka 320-pixel-wide screen) is an open source Objective-C library for iOS, originally written by Joe Hewitt and extracted from the Facebook for iPhone application in a refactoring effort.

When I first considered this library I mainly focused on the UI parts and thus really underestimated at that time what Three20 was concretely providing. In the meanwhile I’ve discovered that Three20 can be thought as a real SDK into the iOS SDK!

Three20 is definitely much more than a bunch of UI components since you’ll find some precious resources splitted among different modules (Core, UI, Network and Style) and extensions (JSON). The README covers the install steps, and the headers provide a decent API documentation. That said prepare yourself to read the code and the samples that ship with it as it is the best way to discover how it works and learn some advanced Objective-C tips and patterns.

So I would like here to quickly illustrate two components.

First: the TTTableViewController. This component acts as a clever, HTTP-aware extension of the iOS’ UITableViewController. Let’s suppose you would like to display a remote collection of items (e.g. a list of pictures) available through a REST/JSON service. Then simply follows the 4 steps below:

  1. logic: subclass TTTableViewController
  2. data container: subclass TTListDataSource – here let’s choose the most appropriate kind of item to display your data
  3. remote data fetcher: subclass TTURLRequestModel – you’ll place here the web service request and response parsing logic thanks to the network and JSON tools
  4. data model: write a simple subclass of NSObject to wrap the properties of your REST resource

That’s it: let’s the base class magic operates! (let’s look at TT code for more advanced usage, including reloading or HTTP cache policy).

Second: the TTNavigator. According to the iOS SDK, you can only present the view associated with a view controller by:

  • adding it as a subview,
  • presenting it in a modal fashion,
  • pushing it onto the navigation stack,
  • enabling the right tab of your tab bar interface

But what if you are at a given level of a navigation stack and would like to namely redirect to any controller of your choice? This is where the power of the TTNavigator operates.

Joe Hewitt indicates that he wanted to organize his applications by “pages” which can be displayed by visiting a URL.

In practice, to make your application navigation-compliant you’ll simply have to define one URL per view controller and setup these URLs into the TTNavigator URLMap on your application delegate.

Then, for the most basic usage a single string can be used to navigate to a given controller thanks to TT additions! e.g. [@"myapp://help" openURL].

In most cases you’ll need to pass some parameters to your controller, and probably want to control which parent controller should be used once the redirection is completed. Then:

  • implement the initWithNavigatorURL: query: method into your controller – this is where you’ll read the query parameters for your own initializations
  • prepare your query by setting some key/value into an NSDictionary
  • create a TTURLAction object for your URL path and use applyQuery to pass in your parameters
  • use applyParentURLPath (it’s chainable!) if you need to precise a specific parent controller

Believe me or not, the navigator is really one of TT killer features!

If you intend to write an iOS app, I hope this quick intro will convince you to rely on Three20. Then let us know about your success story!