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

1
  • Set animation parameter to NO and then try
    – HG's
    May 3, 2011 at 5:45

1 Answer 1

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]];
7
  • @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 3, 2011 at 1: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 3, 2011 at 1:47
  • Can you show the code that instantiante and calls this controller? May 3, 2011 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 5, 2011 at 5: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. May 5, 2011 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.