12

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?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Harish
  • 2,496
  • 4
  • 24
  • 48

8 Answers8

20

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;
user454322
  • 7,300
  • 5
  • 41
  • 52
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • 1
    Also what I wanted. :-) For the record, you need to implement the protocol on the view controller you navigate back to. – doekman Jun 18 '13 at 19:22
  • This solutions works great and is the most efficient. Be sure that the connection to your navigationController delegate is in -(void) viewDidAppear:(BOOL)animated, otherwise alert also appears when the first viewcontroller launches the first time. – pinyin_samu Oct 10 '13 at 09:35
  • @Morkrom Yes it is called every time a view controller is shown in the navigation controller, its better if you check the view controller parameter to make necessary changes. – Satheesh Oct 15 '13 at 14:05
  • 1
    And use if([viewController isKindOfClass:[CustomViewController class]]){ ..code.. } To specify on what viewcontroller it should show a messsage – Oritm Nov 28 '13 at 13:20
  • This method will be called whenever the View will disappear, not only when the back button is pressed. For example, if you push another view (a modal one for example), this method is called too. – Sébastien Stormacq Oct 02 '14 at 20:24
  • @SébastienStormacq you'd just check the view controller passed to the delegate method against the self.presentingViewController property then, to make sure the change was triggered by the back button (most likely). – valeCocoa Jul 12 '17 at 23:10
8

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
}
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
  • K guys thanks, that's the second option, is there any way to detect the back button action of UINavigationController..? – Harish Jan 10 '13 at 10:56
  • What about `b`? Fix it please. – Lorenzo B Jan 10 '13 at 10:58
  • @HarishSaran What do you mean? Do you need to go back when you click on the alert view? – Lorenzo B Jan 10 '13 at 11:01
  • Great suggestion, this worked for me as I'm doing a custom navigation controller and couldn't implement the delegate callbacks as the correct answer. However, I could *not* set `hidesBackButton = YES` as this would cause some visual artifacts when manually pushing new `UINavigationItem`'s manually. – Maurizio May 18 '13 at 18:10
  • Note, you need to to put "self.navigationItem.hidesBackButton = YES" on the parent viewcontroller before you present the viewcontroller with the custom back button. – Elijah Nov 04 '14 at 00:49
7

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];
}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
2

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...
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
0

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

spider1983
  • 1,098
  • 1
  • 7
  • 14
0

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];
}
Vladimir
  • 388
  • 6
  • 13
0
extension ViewController: UINavigationControllerDelegate {
// when the self != viewcontroller ,it's mean back
     func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
          if self != viewController { 
             // your code
           }
}}
JefferyYu
  • 1
  • 1
-1

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

Prajwal Udupa
  • 860
  • 5
  • 28