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 :(
Asked
Active
Viewed 1,423 times
2

Sanoj Kashyap
- 5,020
- 4
- 49
- 75

user1679847
- 233
- 1
- 4
- 11
-
do you want pop up viewcontroller? or display alertview on button action? – Romit M. Sep 18 '12 at 09:47
-
create a back button with custom method of showing the alert and add to navbar implement pop in the alertviewdelegate – Lithu T.V Sep 18 '12 at 10:13
4 Answers
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
-
Ok I think this is the correct way and It works perfectly! Is it possible to make the UIBarButtonItem looks like the Back Button? – user1679847 Sep 18 '12 at 10:07
-
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
-
Would be nice if you mentioned the source with a link to the answer. http://stackoverflow.com/a/3445994/876283 – iNoob Sep 18 '12 at 10:02
-
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