A tree of data:
UINavigationController atop a series of UITableViewControllers

   

The states, counties, and cities are organized into a family tree. It’s strange that we need two different technologies to navigate the tree: a UITableView and a UITableViewController to move vertically (New York → New Jersey → Connecticut) and a UINavigationController to move horizontally (state → county → city).

Source code in Tree.zip

  1. main.m
  2. Class TreeAppDelegate
  3. Class Model is a subclass of NSObject.
  4. Class TableViewController is a subclass of UITableViewController.

Model-View-Controller

The model

The model is a tree in class Model. The root of the tree is named “United States”. The tree contains sub-trees. The sub-trees contain of sub-sub-trees, down to any level. All of the trees, sub-trees, sub-sub-trees, etc., are called trees.

Each tree is an NSArray. The first element of the NSArray is an NSString giving the name of the tree. The remaing element(s), if any, are the subtrees under that tree. It’s just like programming in the language Lisp.

The controllers and their views

The application delegate creates a UINavigationController, which manages a stack of controllers underneath it. See Navigate. Each controller in this stack is a TableViewController. Each TableViewController has a UITableView underneath it.

No Edit/Done button

We never uncommented the button in the viewDidLoad method of the view controller.

Things to try

  1. Add more states, counties, and cities to the model in Model.m. Can you add extra levels: US, Canada and Mexico? The First Ward, the Second Ward in Yonkers?

  2. Change the tree to an outline of the course, similar to the following.
    	tree = BEGIN @"Universe",
    		BEGIN @"iPhone Apps INFO1-CE9236 and INFO1-CE9704",
    			BEGIN @"Hello, world"
    				BEGIN @"The Objective-C language"
    				END,
    				BEGIN @"Create an Xcode project"
    				END,
    				BEGIN @"Create a class of objects"
    				END
    			END,
    
    			BEGIN @"Still-life 2D graphics"
    				BEGIN @"Japan"
    				END,
    				BEGIN @"Manhattan"
    				END
    				BEGIN @"America"
    				END
    			END,
    
    			BEGIN @"Collections, touches, and animations"
    			END,
    
    			BEGIN @"Gesture Recognition"
    			END,
    
    			BEGIN @"Graphics that respond to a touch"
    			END,
    
    			BEGIN @"Read and write a file"
    			END,
    
    			BEGIN @"A control and the target/action pattern"
    			END,
    
    			BEGIN @"View controllers and CADisplayLink animation"
    			END,
    
    			BEGIN @"View controllers that manage other view controllers"
    			END,
    
    			BEGIN @"Special types of view controllers"
    			END,
    
    			BEGIN @"Nib files and the Interface Builder"
    			END,
    
    			BEGIN @"View classes"
    			END,
    
    			BEGIN @"UITableView"
    			END,
    
    			BEGIN @"UIWebView"
    			END,
    		END
    	END;
    

  3. Let the user edit the tree. Start with an Edit/Done button in the navigation bar.