开发者

Can't add a rightBarButtonItem to a NavigationBar

开发者 https://www.devze.com 2023-03-26 08:57 出处:网络
I\'m trying to build an app (for practice) that uses CoreData and different view controllers. I\'m currently blocked, because I can\'t manage to add a rightBarButtonItem to a navigationBar.

I'm trying to build an app (for practice) that uses CoreData and different view controllers. I'm currently blocked, because I can't manage to add a rightBarButtonItem to a navigationBar. Here is the code I'm using (in the AppDelegate, application:didFinishLaunchingWithOptions:).

PersoneTableViewController *ptvc = [[PersoneTableViewController alloc] initWithStyle:UITableViewStylePlain];
ptvc.managedObjectContext = self.managedObjectContext;
ptvc.title = @"Persone";

UINavigationController *navCon = [[UINavigationController alloc] init];
[navCon pushViewController:ptvc animated:NO];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                                  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                  target:self
                                  action:@selector(addPressed)];
navCon.navigationItem.rightBarButtonItem = barButtonItem;
[barButtonItem release];

self.windo开发者_运维百科w.rootViewController = navCon;

[navCon release];

[self.window makeKeyAndVisible];
return YES;

But the rightBarButtonItem is not showed if I run the app in the simulator.


A UInavigationController will use the navigationItem from it's topmost view controller (and second to topmost for the back button). Not it's own navigationItem as in your example.

0

精彩评论

暂无评论...
验证码 换一张
取 消