iPhone - dismiss multiple ViewControllers

后端 未结 22 2911
粉色の甜心
粉色の甜心 2020-11-28 04:46

I have a long View Controllers hierarchy;

in the first View Controller I use this code:

SecondViewController *svc = [[SecondViewController alloc] i         


        
相关标签:
22条回答
  • 2020-11-28 05:19

    In swift 4 And Xcode 9 This will helps you.

    var vc : UIViewController = self.presentingViewController!
            while ((vc.presentingViewController) != nil) {
                vc = vc.presentingViewController!
            }
            vc.dismiss(animated: true, completion: nil)
    

    Enjoy !!! :)

    0 讨论(0)
  • 2020-11-28 05:20

    Dismiss the top VC animated and the other ones not. If you hace three modal VC

    [self dismissModalViewControllerAnimated:NO]; // First
    [self dismissModalViewControllerAnimated:NO]; // Second
    [self dismissModalViewControllerAnimated:YES]; // Third
    

    EDIT: if you want to do this only with one method, save you hierarchy into an array of VC and dismiss the last object animated and the other ones not.

    0 讨论(0)
  • 2020-11-28 05:24

    I found the solution.

    Of course you can find the solution in the most obvious place so reading from the UIViewController reference for the dismissModalViewControllerAnimated method ...

    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.

    so it's enough to call the dismissModalViewControllerAnimated on the target View. I used the following code:

    [[[[[self parentViewController] parentViewController] parentViewController] parentViewController] dismissModalViewControllerAnimated:YES];
    

    to go back to my home.

    0 讨论(0)
  • 2020-11-28 05:24

    Simple recursive closer:

    extension UIViewController {
        final public func dismissEntireStackAndSelf(animate: Bool = true) {
            // Always false on non-calling controller
            presentedViewController?.ip_dismissEntireStackAndSelf(false)
            self.dismissViewControllerAnimated(animate, completion: nil)
        }
    }
    

    This will force close every child controller and then only animate self. You can toggle for whatever you like, but if you animate each controller they go one by one and it's slow.

    Call

    baseController.dismissEntireStackAndSelf()
    
    0 讨论(0)
  • 2020-11-28 05:25

    In Swift:

    self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    
    0 讨论(0)
  • 2020-11-28 05:26

    If you are using all are Model view controller you can use notification for dismissing all preseted view controller.

    1.Register Notification in RootViewController like this

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(dismissModelViewController)
                                                 name:dismissModelViewController
                                               object:nil];
    

    2.Implement the dismissModelViewController function in rootviewController

    - (void)dismissModelViewController
    {
        While (![self.navigationController.visibleViewController isMemberOfClass:[RootviewController class]])
        {
            [self.navigationController.visibleViewController dismissViewControllerAnimated:NO completion:nil];
        }
    }
    

    3.Notification post every close or dismiss button event.

       [[NSNotificationCenter defaultCenter] postNotificationName:dismissModelViewController object:self];
    
    0 讨论(0)
提交回复
热议问题