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.
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 .
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:
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]; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With