Reputation: 6823
I would like to better understand the use of UINavigationController's.
I have setup a new project with a UINavigationViewController, as well as two other view controllers.
In my app delegate I have the following:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
firstViewController = [[NCTFirstViewController alloc] initWithNibName:@"NCTFirstViewController" bundle:nil];
navController = [[NCTNavViewController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
Within my UINavigationController.m file I can set the title and set up buttons, but this does not work.
This only works if I setup the self.navigtionController items in the actual View Controller itself. Is this correct, or is there something I should be doing in the UINavigationController to get this to work.
Ideally I am looking for a UINavigationController that handles all the pushing to other controllers. In the long run it would be used as a menu system. So if the user clicks a button at the top, they are pushed to a new View Controller but without the back option, simply the same menu items in the navigation bar at the top, which shows a new center view controller.
The issue I am having is understanding how this is setup. If I have to setup this in the view controllers itself, would they all not duplicate the same code. Of course I could setup all the 'movement' in the AppDelegate, but this doesn't seem correct and would pack up the App Delegate itself.
Upvotes: 0
Views: 4768
Reputation: 25740
If you look at the UINavigationController class reference, it says (emphasis added by me, but you should read it all so that you understand how this works):
Updating the Navigation Bar
When the user changes the top-level view controller, whether by pushing or popping a view controller or changing the contents of the navigation stack directly, the navigation controller updates the navigation bar accordingly. Specifically, the navigation controller updates the bar button items displayed in each of the three navigation bar positions: left, middle, and right. Bar button items are instances of the UIBarButtonItem class. You can create items with custom content or create standard system items depending on your needs. For more information about how to create bar button items, see UIBarButtonItem Class Reference.The bar button item on the left side of the navigation bar allows for navigation back to the previous view controller on the navigation stack. The navigation controller updates the left side of the navigation bar as follows:
- If the new top-level view controller has a custom left bar button item, that item is displayed. To specify a custom left bar button item, set the leftBarButtonItem property of the view controller’s navigation item.
- If the top-level view controller does not have a custom left bar button item, but the navigation item of the previous view controller has a valid item in its backBarButtonItem property, the navigation bar displays that item.
- If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack. (If there is only one view controller on the navigation stack, no back button is displayed.)
The navigation controller updates the middle of the navigation bar as follows:
- If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item.
- If no custom title view is set, the navigation bar displays a label containing the view controller’s default title. The string for this label is usually obtained from the title property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the title property of the view controller’s navigation item instead.
The navigation controller updates the right side of the navigation bar as follows:
- If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the view controller’s navigation item.
- If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.
The navigation controller updates the navigation bar each time the top view controller changes. Thus, these changes occur each time a view controller is pushed onto the stack or popped from it. When you animate a push or pop operation, the navigation controller similarly animates the change in navigation bar content.
So, to do what you want, you do need to set the title and buttons in each view controller. The easiest way to set the title is to set it in the storyboard/xib, or in code when you create it.
You should also replace the root view controller instead of pushing the view controller onto the navigation stack so that you don't keep adding view controllers to the stack. This also avoids displaying the back button, and you won't need to explicitly get rid of it. You would do this by using the setViewControllers:animated:
method like this:
[self.navigationController setViewControllers:[NSArray arrayWithObject:theNewViewController] animated:YES];
Upvotes: 3
Reputation: 957
There is only one NavigationController and this NavigationController controls all the ViewControllers. So barButtonItems and titles are set in each ViewController, you should not set them directly to a NavigationController.
If you want to push a new ViewController and won't push back, I think this may cause some problem because the NavigationController is using a Stack to handle all the ViewControllers. Seems that you will push a ViewController in the stack but not pop it, and maybe you will get some behavior you don't want.
If you still want to implement this, I think you cannot avoid do similar setting in different ViewControllers.
Upvotes: 0