1

I have a 5-6 view controllers in my application and I am using navigation controller for that. Now what I am trying to do is : From entering one view controller to another, a UIAlertView box comes and on clicking "No", it should pop the view controller and goes to the previous one. I have tried PopToViewController: animated and PopViewController:Animated but none of them is working. Can anybody tell me if that is possible? and if yes, how?

I can print anything on console when pressing "No" on alert box but view is not getting popped. Any suggestions?

Thanks Vik

Vik
  • 488
  • 2
  • 10
  • 19

1 Answers1

0

The answer assumes that by "view" you are referring to a UIViewController (as you can only push/pop controllers) AND that you are calling this controller by the pushViewController:animated: selector on your navigation controller.

Given that, you have to implement UIAlertViewDelegate in your controller .h file

@interface MyController : UIViewController <UIAlertViewDelegate> {

}

@end

Then, make sure to check the button indexes properly when you create the UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"No", nil];

[alert show];
[alert release];

In the example above, the "OK" button would be index 0 and the "No" button, the index 1. So when you implement the alertview:clickedButtonAtIndex: delegate method you can check if the user tapped the right button and then perform the controller pop action.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) { //button NO licked
        [self.navigationController popViewControllerAnimated:YES];
    }

}

EDIT:

Make sure you instantiate your UINavigationController, otherwise, your controller navigationController property will be nil

tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
NSMutableArray *controllers = [[NSMutableArray alloc] initWithCapacity:1];
ownerController *oController = [[ownerController alloc] init];

//navigation controller creation
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: oController];
[oController release]; 
//adds navigation controller to collection of controllers
[controllers addObject:nav]; 

[tabBarController setViewControllers:controllers]; 
[controllers release];   
[self.view addSubview:[tabBarController view]];
Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
  • @Felipe - I am doing exactly the same thing. and I am able to print stuff on console but popViewControllerAnimated: function is not working. Below is the relevant code: – Vik May 03 '11 at 01:45
  • -(void)viewDidLoad { [super viewDidLoad]; self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; UIAlertView *alertOwner = [[UIAlertView alloc] initWithTitle:@"Are you Owner?" message:@"Owner Please" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil]; [alertOwner show]; [alertOwner release]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex == 0) { [self.navigationController popViewControllerAnimated:YES]; }} – Vik May 03 '11 at 01:47
  • Can you show the code that instantiante and calls this controller? – Felipe Sabino May 03 '11 at 10:42
  • I think that is what you wanted to see: tabBarController = [[UITabBarController alloc] init]; tabBarController.view.frame = CGRectMake(0, 0, 320, 460); NSMutableArray *controllers = [[NSMutableArray alloc] initWithCapacity:1]; ownerController *oController = [[ownerController alloc] init]; [controllers addObject:oController]; [oController release]; [tabBarController setViewControllers:controllers]; [controllers release]; [self.view addSubview:[tabBarController view]]; – Vik May 05 '11 at 05:46
  • Is ownerController a class that inherits UINavigationController? If not, that is your problem, as you NavigationController property is null when you call the pop method...I will edit the answer so that you can see how to create the navigation controller. – Felipe Sabino May 05 '11 at 11:21
  • @Felipe - but ownerController is the third view controller and the root controller inherits UINavigationController and then takes me to the 2nd controller and from there i come to 3rd controller that is ownerController. Now do I have to declare another navigationController for that? Thanks – Vik May 05 '11 at 21:16
  • I didn't understand how your controllers are arranged. The only think I know by the code you posted is that ownerController has no navigaton controller associated with it. You can either creat a new one or, instead of doing [self.view addsubview...] (btw, what is self???) you could do [self.navigatonController pushViewController...] (if, of course, self is a UIViewController and has a navigation controller associated with it) – Felipe Sabino May 06 '11 at 10:47