2

I have a Navigation Controller and when I press the back button I don't want to pop the view but I want to show an UIAlertView.I just want to pop the view after having make a choice on the UIAlertView! How can I do? I tried to catch the event 'back button pressed' but with no results :(

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
user1679847
  • 233
  • 1
  • 4
  • 11

4 Answers4

3

Create a UIBarButtonItem inside viewDidload and added it to the navigation bar

UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;

create a method which will be called when the backButton is clicked

-(void)backButtonClicked{
       UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Pop View!" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
       [alert show];
}

write this UIAlertView delegate method which will be called when the button in the UIAlertView is clicked

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
      if (buttonIndex==1) {
             [self.navigationController popViewControllerAnimated:YES];
      }
}

NOTE: Dont forget to add UIAlertView delegate in the header file <UIAlertViewDelegate>

Neo
  • 2,807
  • 1
  • 16
  • 18
1

You can check for back-button press in the viewWillDisappear method like this (got it from here):

-(void)viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
        // back button was pressed.  We know this is true because self is no longer
        // in the navigation stack.
    }
    [super viewWillDisappear:animated];

Hope this helps!

Community
  • 1
  • 1
Thermometer
  • 2,567
  • 3
  • 20
  • 41
0

Implement pop in UIAlertview delegate method clickedButtonAtIndex: The back button press need to implement only showing the UIAlertview

refer this

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
0

follow this code

in button action.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Do you want to open?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];

//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0)//NO
    {
        //your code
    }
    else if(buttonIndex==1)//YES
    {
        // your code
    }
}
Romit M.
  • 898
  • 11
  • 28