iOS ViewController Present/dismiss

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shengpeng3344/article/details/79525663

对于ViewController A 、B、C

A –> B –> C

A推出B,B推出C

1、[A presentViewController:B animated:YES completion:nil];
2、[B presentViewController:C animated:YES completion:nil];

1、我想在C界面,直接回到最开始的A怎么办

下面这句代码只会返回到B

[self dismissViewControllerAnimated:YES completion:nil];

首先想到先dismiss掉C,再dismiss掉B。后面发现还是对视图推出不太了解。UIViewController有两个属性

// The view controller that was presented by this view controller or its nearest ancestor.
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController  NS_AVAILABLE_IOS(5_0);

// The view controller that presented this view controller (or its farthest ancestor.)
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController NS_AVAILABLE_IOS(5_0);

即A present B,则A的presentedViewController就是B,B的presentingViewController就是A。

要实现返回到A,只需要调用A的dismiss就可以了。
我在推出时就获取第一个VC(B)的指针,需要返回A时,B.presentingViewController dismiss就行了。

2、B的两次dissmiss结果不一致

A->B->C后,第一次调用B dismiss会返回到B界面,再次(第二次)调用B dismiss则会返回到A

3、A dismiss 了不就A也就没了吗?

dismiss操作只和present对应,A是最原始的VC,没有VC推出A,调用A的dismiss则会把A所present的视图全部dismiss掉。再次调用本应该返回A.presentingViewController视图,但A.presentingViewController不存在。

测试demo : 链接: https://pan.baidu.com/s/1cxIXMGEPFFZjBt5DkyNxfw 密码: pha5

猜你喜欢

转载自blog.csdn.net/shengpeng3344/article/details/79525663
今日推荐