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.
You can instead adopt the
UINavigationControllerDelegate
protocol in the class which creates theUINavigationController
instance. You can also create thecancelButton
in advance and then implementnavigationController:willShowViewController:animated:
like this,You will have to remember to create and hold the
cancelButton
and not release it. This will also meancancelRequestNewLeave:
will have to be a method in class that creates theUINavigationController
instance which is what it is right now I guess.This is how I did it with
UINavigationController
subclass that is capable of dismissing every viewController pushed into it.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:
like that
CommonViewController.h
CommonViewController.m
FirstViewController.h
FirstViewController.m
SecondViewController.h
SecondViewController.m
note: you can add the code of initializeCartBarButton in the viewDidLoad of CommonViewController and delete this fuction from CommonViewController and from child class's
You can also subclass
UINavigationcontroller
and overide few methods like this:You can add a custom 'Cancel' UIButton directly to the NavigationBar's view instead of using the UIBarButtonItem.
The normal way to do this is to add that cancel button to the navigationItem of every single view controller in your navigation stack. The above approach can make it simpler by allowing you to write less code, but it is a tiny bit of a hack.