views:

68

answers:

2

Hi all My small app is growing more sprawling and I'm looking for some best practice advice on the management/ownership of view controllers and navigation controllers.

Here's what I'm doing now:

  1. AppController is a singleton that creates and owns a UINavigationController instance. The app controller, and thus the navigation controller, can be globally accessed via a +sharedController like method.
  2. Every view controller in the app that wishes to push a new view controller, basically does this:

    NextViewController * nextViewController = [[NextViewController alloc] init]; [[[AppController sharedController] navigation] pushViewController:nextViewController ...]; [nextViewController release];

In this way, all "leaf" views are responsible for creating the next view over and pushing it, and the navigation controller lives in one place that everyone can get to.

But I cooked this up myself. Since navigation through view controllers is such a critical piece of architecture, I'm wondering if anyone has a better or more thoughtful approach here.

Thanks.

+2  A: 

Why not use self.navigationController in those views that need to push? The only thing you need to do in the AppController is push the initial view on the navigation controller.

St3fan
+4  A: 

Every view controller has a navigationController property. If the UIViewController is part of a navigation stack, this property is set so you can grab a reference to the UINavigationController. So, instead of having to reference the AppController (or perhaps even having one at all - you can just put this in the AppDelegate), you can just do something like this:

NextViewController * nextViewController = [[NextViewController alloc] init];
[self.navigationController pushViewController:nextViewController ...];
[nextViewController release];
bpapa
Well, that makes plenty of sense. How did I miss this? Thanks!
quixoto