iPhone - dismiss multiple ViewControllers

后端 未结 22 2914
粉色の甜心
粉色の甜心 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:41

    A swift version with some additions based on this comment

    func dismissModalStack(viewController: UIViewController, animated: Bool, completionBlock: BasicBlock?) {
        if viewController.presentingViewController != nil {
            var vc = viewController.presentingViewController!
            while (vc.presentingViewController != nil) {
                vc = vc.presentingViewController!;
            }
            vc.dismissViewControllerAnimated(animated, completion: nil)
    
            if let c = completionBlock {
                c()
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:44
    [[self presentingViewController]presentingViewController]dismissModalViewControllerAnimated:NO];
    

    You can also implement a delegate in all controllers you want to dismiss

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

    Try this..

    ThirdViewController *tvc = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.view addsubview:tvc];    
    [tvc release];
    
    0 讨论(0)
  • 2020-11-28 05:46

    Swift 3 extension based upon the above answers.

    Principle for a stack like that : A -> B -> C -> D

    • Take a snapshot of D
    • Add this snapshot on B
    • Dismiss from B without animation
    • On completion, dismiss from A with animation

      extension UIViewController {
      
          func dismissModalStack(animated: Bool, completion: (() -> Void)?) {
              let fullscreenSnapshot = UIApplication.shared.delegate?.window??.snapshotView(afterScreenUpdates: false)
              if !isBeingDismissed {
                  var rootVc = presentingViewController
                  while rootVc?.presentingViewController != nil {
                      rootVc = rootVc?.presentingViewController
                  }
                  let secondToLastVc = rootVc?.presentedViewController
                  if fullscreenSnapshot != nil {
                      secondToLastVc?.view.addSubview(fullscreenSnapshot!)
                  }
                  secondToLastVc?.dismiss(animated: false, completion: {
                      rootVc?.dismiss(animated: true, completion: completion)
                  })
              }
          }
      }
      

    A little flickering on simulator but not on device.

    0 讨论(0)
提交回复
热议问题