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:
- 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. 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 o开发者_Go百科r more thoughtful approach here.
Thanks.
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];
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.
精彩评论