How to dismiss the two or more dismissModalViewController?
Asked Answered
U

5

5

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

Ulm answered 25/11, 2010 at 10:50 Comment(2)
To pop to the root view controller you can just do this: [self.navigationController popToRootViewControllerAnimated:YES]; instead of your way, which is a bit faffy.Ambrosia
it is present modal view controllers, how can i pop itUlm
U
2
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.

Ulm answered 1/12, 2010 at 7:44 Comment(0)
S
5

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.

Snoddy answered 29/11, 2010 at 15:3 Comment(0)
R
4

Use this below code

[[[self presentingViewController] presentingViewController]  dismissModalViewControllerAnimated:YES];
Roddy answered 18/10, 2012 at 10:21 Comment(0)
F
3

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.

Firebrand answered 1/6, 2011 at 18:54 Comment(0)
U
2
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.

Ulm answered 1/12, 2010 at 7:44 Comment(0)
P
1

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];
         }];
     }
}
Parasitize answered 16/9, 2012 at 23:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.