I think this should be a simple one but I'm left scratching my head. I have an app with multiple ViewControllers setup in a storyboard. I want each of these to have the same navigation bar at the top of every view but this navigation controller to have the same buttons on all views. The navigation controller is setup in AppDelegate and the buttons are added in the ViewDidLoad of each ViewController (as per an answer I received on here for another question) but at the moment, the buttons (and the title) aren't showing.
Where the UINavigationController is setup - AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];
[self.window setRootViewController:navVC];
[_window makeKeyAndVisible];
return YES;
}
One of the Views:
- (void)viewDidLoad
{
[self setTitle:@"Congress app"]; // < This does not set the title of the NavController
UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];
//self.navigationItem.leftBarButtonItem = showMenuButton;
//self.navigationItem.rightBarButtonItem = showSettingsButton;
self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;
UINavigationController *currentNav;
UIViewController *VCname;
NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
[super viewDidLoad];
}
I have a couple of NSLogs that show what (if anything) is in the navigation controller, it shows: and which intimates that buttons are being added but not shown?
EDIT:
I have just remembered that there is a ViewController (InitViewController.m) that is fired before the VC in the above code. The NavigationController is allocated here:
- (void)viewDidLoad
{
[super viewDidLoad];
self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
}
This is likely to be the cause of the problem.
EDIT 2:
Thanks to Saurav Mac I have tracked the problem down to me trying to add the buttons on the 'wrong' view controller. I had the Navigation Controller setup in AppDelegate.m, the first View Controller is InitViewController which then calls MainViewController to be the 'top view controller'. Can't believe I missed that, thanks for the help.