我应该在呈现模式的UIViewController或UIViewController上调用dismissViewController吗? [英] Should I call dismissViewController on the modal UIViewController or the UIViewController that presented it?

查看:155
本文介绍了我应该在呈现模式的UIViewController或UIViewController上调用dismissViewController吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种情况下似乎都可以工作,但是为什么以及如何进行,哪一种是最好的方法?

It seems to work in either case, but why and how, and which is the best way?

推荐答案

为什么和如何

比方说,视图控制器A提出了B,而B又提出了C.

Why and How

Let's say, view controller A has presented B, and B has presented C in turn.

当您调用dismissViewControllerAnimated:completion:时,iOS会检查您正在调用它的视图控制器的presentedViewController属性中是否有子代.如果是这样,则解雇该控制器.

When you call dismissViewControllerAnimated:completion:, iOS checks to see if the view controller you're calling it on has a child in its presentedViewController property. If it does, then it dismisses that controller.

因此,如果在我们的示例中在B上调用它,则它会解散C,但是如果在A上调用它,它会解散B(它带走C).

So if I call it on B in our example, it dismisses C, but if I call it on A, it dismisses B (which takes away C with it).

如果我在C上调用它,则它没有子代.因此,iOS会检查presentingViewController属性.如果已设置,它将要求 it 关闭其控制器.因此,如果我在C上调用它,iOS会将请求发送回B,B将驳回C.

If I call it on C, then it has no child. So then iOS checks the presentingViewController property. If that is set, it asks it to dismiss its controller. So if I call it on C, iOS sends the request back to B, which dismisses C.

因此,在B或C上调用该方法将摆脱C,而在A上调用该方法将摆脱B和C.

So calling the method on either B or C will get rid of C, while calling it on A gets rid of B and C.

每次在出现的东西上在B上调用它.这是

Call it on B, on the thing that presented, every time. This is

  1. 减少混乱,因为您的出席和解雇将在同一地点,并且

  1. less confusing, since your present and dismiss will be in the same place, and

更有前途的证明,因为在将来的某个时候,您可能会让C出现另一个视图控制器(D),然后解雇会突然中断(它将突然开始解雇D,而不是解散自己) .

more future proof, since at some point in the future you may make C present yet another view controller (D), and then the dismissal would suddenly break (it would suddenly start dismissing D, rather than dismissing itself).

这篇关于我应该在呈现模式的UIViewController或UIViewController上调用dismissViewController吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆