How to dismiss the two or more dismissModalViewController?

不问归期 提交于 2019-11-29 10:53:06

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.

Madan Mohan
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.

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];
         }];
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!