I am trying to make an iPhone application that has an optional top level navigation controller.
Adding it is fine, but trying to set the title doesn’t work, unless you try to add your own navigation item, at which point the app crashes with an ‘NSInternalInconsistencyException’ with reason: ‘Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.’
- (void)applicationDidFinishLaunching:(UIApplication *)application {
rootHasNavBar = NO;
if (window && viewController)
{
// Tab Controller is root:
if (!rootHasNavBar)
{
window.rootViewController = viewController; // viewController:UITabBarController
[window makeKeyAndVisible];
}
else
{
// Navigation controller above UITabBarController
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
window.rootViewController = navigationController;
//Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"RentalPoint",nil)];
[ navigationController.navigationBar pushNavigationItem:item animated:YES];
navigationController.toolbarHidden = YES;
[window makeKeyAndVisible];
};
}
How should I do that? Or is there some technical reason why I can’t do that?
Update: The people who suggested simply moving the assignment of a controller title and navigation item title in the controller’s viewDidLoad were on to some internal (and highly unintuitive for new person) aspects of Cocoa architecture that were still quite unfamiliar to me. On the outside looking at a framework like Cocoa that is huge, and complex and which doesn’t let you browse the source code, this is the kind of mystery that I find most difficult. In my other languages and tools, I can always read and step into the code, including my frameworks. here, you can’t, and so you gotta read, read, read, and thankfully there are LOTs of great documentation sources.
Set the navigation title on the init of your UIViewController.
or if you really wanted to hard code it into the applicationDidFinishLaunching routine, you could just do: