如何dismiss多个viewController

如何dismiss多个viewController(当然是建立在已经连续present多个controller的前提下)

今天才真正认识dismissViewControllerAnimated这个方法:

举个例子:

A->B->C->D (->代表present,当前页面是D)

如果想回到B

用[B dismissViewControllerAnimated:YES completion:nil]

这样调用,只会最上面的D会以动画的方式消失,其他中间的C从堆栈中删除,达到回到B的效果。

如何在D获取到B?
D的presentingViewController是C,C的presentingViewController是B,
所以
在D页面的时候
C= D.presentingViewController;
B = C.presentingViewController;

UIViewController *parentVC = self.presentingViewController;

再举个例子A->B

dismissViewControllerAnimated 其实是prespresenting view controller(A)用这个方法来dissmiss B的,而我们平时经常在B页面使用用[self dismissViewControllerAnimated:YES completion:nil];(self是B),其实UIKit会要求A处理这个dismiss。

原文:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

If you present several view controllers in succession, thus building a stack of presented 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.

猜你喜欢

转载自blog.csdn.net/qq_28285625/article/details/110819204
今日推荐