Recent site activity

External Links

My Personal Blog‎ > ‎

Reviewing UINavigationController

posted Dec 7, 2009 7:50 AM by Theresa McClain
When I built Presence 2, I made a fairly significant mistake that has now cost me a lot of time.  I instantiated both a UINavigationController and a custom TableViewController in my app delegate, but I didn't push my TableViewController onto the stack of views that are managed by my UINavigationController.  Instead, this is what I did:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    personListViewController = [[PersonListViewController alloc] initWithStyle:UITableViewStylePlain];
    personListViewController.view.frame = [UIScreen mainScreen].applicationFrame;    
    [window addSubview:personListViewController.view];
    
    navigationController = [[UINavigationController alloc] initWithRootViewController:personListViewController];
    [window addSubview:navigationController.view];

    [window makeKeyAndVisible];

}

Well, that is wrong.  Pragmatically, it seemed to work, except that by the time I started Presence 3 I discovered that in my TableViewController implementation, this was causing problems; not the least of which was that on initial load, viewWillAppear in my TableViewController's implementation was being called twice.  Since I was using viewWillAppear to call a method that would instantiate a NSInvocationOperation and add it to an NSOperationQueue, it was causing some problems.

Okay so lesson learned (twice).  In order to add a UITableViewController to a UINavigationView, you need to push the UITableViewController's view onto the stack of views managed by the UINavigationView.  Here's the correct code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    navigationController = [[UINavigationController alloc] init];
    personListViewController = [[PersonListViewController alloc] initWithStyle:UITableViewStylePlain];
    [navigationController pushViewController:personListViewController animated:NO];
    [window addSubview:navigationController.view];
    
    [window makeKeyAndVisible];
}