Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding same button to all view controllers in UINavigationController

I have a UINavigationController (to use like a wizard page) which I create programmatically and I need to display a "Cancel" button to cancel the process in any UIViewController.

Creating the UINavigationController:

FirstVC *firstVC = [[[FirstVC alloc] initWithNibName:@"FirstPage" bundle:nil] autorelease]; firstVC.delegate = self;  navigationController = [[UINavigationController alloc] initWithRootViewController:firstVC]; [self.view addSubview:navigationController.view]; 

Adding Cancel Button:

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelRequestNewLeave:)]; navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton; [cancelButton release]; 

But when I push a second page to UINavigationController the cancel button is not shown on the UINavigationBar. If I go back to first page, the cancel button is there. So, apparently the button is added only for the first view. I believe this is because I'm not subclassing UINavigationController, because I need to use it in a subview. But I don't know how to set the rightBarButtonItem in a UINavigationController which is created programmatically.

navigationController.topViewController.navigationItem.rightBarButtonItem = cancelButton; 

Can someone shed a light on this?

Thanks in advance.

like image 499
Ozay Avatar asked Jun 17 '11 16:06

Ozay


People also ask

How do I add a view controller to my navigation controller?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


2 Answers

The navigation item is per view controller. The navigation bar draws its contents from the navigation item of the view controller whose view it's currently framing, which corresponds to the view controller at the top of the navigation controller's stack.

You basically need each view controller to stick a cancel button in its navigation item. You can do any of the following:

  • Copy-paste the code into all relevant view controllers.
  • Move the code into a utility function or class and call that.
  • Create a common superclass for all relevant view controllers that handles setting up the cancel button for its subclasses.
like image 87
Jeremy W. Sherman Avatar answered Oct 22 '22 01:10

Jeremy W. Sherman


You can also subclass UINavigationcontroller and overide few methods like this:

- (id)initWithRootViewController:(UIViewController *)rootViewController {     self = [super initWithRootViewController:rootViewController];     if (self) {         [self setCloseButtonToController:rootViewController];     }     return self; }  - (void)dismissController {     [self dismissViewControllerAnimated:YES completion:nil]; }  - (void)setCloseButtonToController:(UIViewController *)viewController {     UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];     [viewController.navigationItem setRightBarButtonItem:closeItem]; }  - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {     [super pushViewController:viewController animated:animated];      [self setCloseButtonToController:viewController];  } 
like image 29
Rafał Wójcik Avatar answered Oct 22 '22 02:10

Rafał Wójcik