I have an two controllers
1st is self
and 2nd is maincontroller
, where I'm pushing maincontroller
in stack, so the back button is automatically coming.
Here I need to make an alert when the user presses the back button.
How can I do this?
I have an two controllers
1st is self
and 2nd is maincontroller
, where I'm pushing maincontroller
in stack, so the back button is automatically coming.
Here I need to make an alert when the user presses the back button.
How can I do this?
Or you can use the UINavigationController
's delegate methods. The method willShowViewController
is called when the back button of your VC is pressed.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
First hide the back button by using
self.navigationItem.hidesBackButton = YES;
and then create your own Custom Button:
UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(popAlertAction:)];
self.navigationItem.leftBarButtonItem=backBtn;
[backBtn release];
and your selector is here:
- (void)popAlertAction:(UIBarButtonItem*)sender
{
//Do ur stuff for pop up
}
Best and Easiest way
Try putting this into the view controller where you want to detect the press:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
}
[super viewWillDisappear:animated];
}
Create your own UIBarButtonItem
and set it as the leftBarButtonItem
in viewDidLoad
method of mainController
.
For example (here I used a system item but you can also create a different one, see class reference for details).
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showAlertView:)];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;
// only if you don't use ARC
// [leftBarButtonItem release];
where
- (void)showAlertView:(id)sender
{
// alert view here...
}
add a custom back button with an action and set your alert in that action method.You can add your custom back button from here: http://www.applausible.com/blog/?p=401
viewControllerCount - is the var that holds the number of viewControllers previously was in the UINavigationController. Then, we check if viewControllerCount > [viewControllers count] if so, we know that we will get back (i.e. Back button imitation).
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
NSArray *viewControllers = [navigationController viewControllers];
if (viewControllerCount > [viewControllers count])
{
// your code
}
viewControllerCount = [viewControllers count];
}
create a button and give the button action as follows.
[self alert];
and when the alert is displayed, after tapping over yes
[self.navigationController popViewController];
after this,
self.navigationController.LeftBarButton = myButton;
this may help