rightBarButtonItem does not appear in Navigation Bar iOS
B

1

16

I'm having problems displaying the rightBarButtonItem of the Navigation Bar - I'm attempting to create it programmatically in the Application Delegate, where my UINavigationController is set up.

Code is as follows:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
RSCListViewController *list = [[RSCListViewController alloc] initWithStyle:UITableViewStylePlain];
self.navController = [[UINavigationController alloc] initWithRootViewController:list];

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"+"
                                                              style:UIBarButtonItemStylePlain
                                                             target:list
                                                             action:@selector(addPressed:)];

self.navController.navigationItem.rightBarButtonItem = barButton;

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

[DatabaseManager openDatabase];

return YES;
}

Running the application, no button item appears on the navigation bar.

I'm not sure whether I have missed something obvious - my attempts to rectify the problem using related Stack Overflow threads haven't yielded any success.

Any help appreciated.

Brooking answered 12/9, 2012 at 20:23 Comment(1)
everything else displays correctly?Suprematism
C
43

You need to attach your bar button item to your custom view controller, not to the navigation controller. From Updating the Navigation Bar:

In addition, the navigation controller object builds the contents of the navigation bar dynamically using the navigation items (instances of the UINavigationItem class) associated with the view controllers on the navigation stack. To change the contents of the navigation bar, you must therefore configure the navigation items for your custom view controllers.

(...)

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.

Therefore, replace:

self.navController.navigationItem.rightBarButtonItem = barButton;

with:

list.navigationItem.rightBarButtonItem = barButton;
Coarsegrained answered 12/9, 2012 at 20:42 Comment(4)
Thanks - had assumed that the elements displayed on the navigation bar were tied to the navigation controller rather than the view at the top of the stack... but now I think about it, this wouldn't make much sense as the navigation bar will often change as a result of which view is being displayed. Thanks again for the help.Brooking
For some reason that does not work when the VC whose navigationItem you set rightBarButtonItem is the rootViewController :-( But, as you said, it works fine whenever the vc with @navigationItem.rightBarButtonItem = whatever;" is pushed onto nav stack though.Multiply
What is list?Cede
No, you should not use the class name. The variable list refers to an instance of the class. In the question, the rightBarButtonItem is set from outside the class.Coarsegrained

© 2022 - 2024 — McMap. All rights reserved.