Question

I need to dismiss the two modal view controllers, I know how to pop two or more view controllers

        UINavigationController* navController = self.navigationController;
    NSArray *array=[navController viewControllers];
    UIViewController* controller = [navController.viewControllers objectAtIndex:0];
    [navController popToViiewController:controller animated:YES];

This is how i can navigate back to my first view but if there are two or more dismiss modal view then how can i navigate back

please help me, Thank you, Madan Mohan

Was it helpful?

Solution 4

UINavigationController* navController = self.navigationController;
NSArray *viewControllers=[navController viewControllers];
UIViewController* controller = [viewControllers objectAtIndex:0];
[navController popToViewController:controller animated:YES];

if you set the object at index 0 in the above code its gonna take you to first view which is a push view controller.

1)Rootview--->moodalview1--->moodalview2--->moodalview3 if you use above code then you wiil be in root view.

2)Rootview--->Pushview1---->moodalview1--->moodalview2----->moodalview3. if you use above code you will be in the PushView.

OTHER TIPS

From the docs for -[UIViewController dismissModalViewController]:

If you present several modal view controllers in succession, and thus build a stack of modal view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

Use this below code

[[[self presentingViewController] presentingViewController]  dismissModalViewControllerAnimated:YES];

I use the following utility static method to simulate popToRootViewController for a stack of modals:

// Util.m
+ (void)popModalsToRootFrom:(UIViewController*)aVc {
    if(aVc.parentViewController == nil) {
        return;
    }
    else {
        [Util popModalsToRootFrom:aVc.parentViewController];  // recursive call to this method
        [aVc.parentViewController dismissModalViewControllerAnimated:NO];
    }
}

You use it like this:

[Util popModalsToRootFrom:aViewController];

If you want something more advanced, you could do this:

+ (void)popModalsFrom:(UIViewController*)aVc popCount:(int)count {
    if(aVc.parentViewController == nil || count == 0) {
        return;
    }
    else {
        [Util popModalsFrom:aVc.parentViewController popCount:count-1];  // recursive call to this method
        [aVc.parentViewController dismissModalViewControllerAnimated:NO];
    }
}

Then pass the number of modals to pop, or just -1 to pop all the way to the root.

For iOS 5, support of animation==YES (views will hide in sequence) and completion block:

+ (void)dismissAllVCsForVC:(UIViewController *)VC animated:(BOOL)animated completion:(BPSimpleBlock)completion {
    if (VC.presentedViewController == nil) {
        if (completion) {
            completion();
        }
    } else {
        [BaseViewController dismissAllVCsForVC:VC.presentedViewController
                                        animated:animated
                                      completion:
         ^{
             [VC dismissViewControllerAnimated:animated completion:completion];
         }];
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top