Search code examples
iosios7uinavigationbaruinavigationitem

How to add a right NavigationItem to a navigationViewController with root viewController


I see that UINavigationItem *navigationItem is readonly property of UINavigationViewController.

TestViewController *test = [[TestViewController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:tableController];
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"Me" style:UIBarButtonItemStyleDone     target:self action:@selector(done:)];
[navigationController.navigationItem setRightBarButtonItem:item]; // NOT WORK


UIPopoverController *pop = [[UIPopoverController alloc]initWithContentViewController:navigationController]; 
pop.popoverContentSize = CGSizeMake(500, 300);
CGRect rect = CGRectMake(300, 400, 50, 50);
[pop presentPopoverFromRect:rect inView:senderButton.superview permittedArrowDirections:0 animated:YES];

Solution

  • You need to access the navigationItem property of the viewController, not of the navigation controller.

    [test.navigationItem setRightBarButtonItem:item]