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 totally different technologies to navigate the tree: a UITableView and a UITableViewController to move in one direction (Connecticut → New York → New Jersey) and a UINavigationController to move in the other direction (state → county → city).

Source code in Tree.zip

  1. Class AppDelegate
  2. Class Model is a subclass of NSObject. For JSOn format, see the weather exercise in Hello.
  3. Class TableViewController is a subclass of UITableViewController.
  4. tree.json is a text file.

Create the project

Main.storyboard creates the UINavigationController. See the last exercise in States for adding a text file (such as tree.json) to a project.

Model-View-Controller

The model

The model is a tree in class Model. The root of the tree is named “Universe”. 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 array. The first element of the array is a String 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 Edit/Done 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. Display the directories and files visible to the app, starting at the app’s home directory.

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