本文介绍了在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
UINavigationController
有 3 个控制器,它们听起来都与我非常相似:topViewController
、visibleViewController
和 presentedViewController
.
UINavigationController
has 3 controllers that all sound very similar to me: topViewController
, visibleViewController
, and presentedViewController
.
您在哪些场景中使用这些控制器?
Which scenarios do you use each of these controllers in?
推荐答案
- topViewController - 使用 UINavigationController's
pushViewController(_:animated:)
方法推送到 UINavigationController 上的最后一个视图控制器.将之前的控制器推开并替换它. - presentedViewController - 呈现在另一个顶部的视图控制器(基本上它覆盖了另一个视图控制器而不是将其推开).代替 UINavigationController's
pushViewController(_:animated:)
你使用 UIViewController'spresent(_:animated:completion:)
方法.注意:呈现的视图控制器也称为模态视图控制器,可以在没有 UINavigationController 的情况下使用. - visibleViewController 可以与 topViewController 或presentedViewController 相同.如果您最后推到 UINavigationController 上,它将与 topViewController 相同.如果你最后在 UIViewController 上呈现,它会和presentedViewController 一样.
- topViewController - last view controller pushed onto the UINavigationController using UINavigationController's
pushViewController(_:animated:)
method. Pushes the previous controller out of it's way and replaces it. - presentedViewController - view controller that is presented over top of another (basically it covers up another view controller instead of pushing it out of the way). INSTEAD of UINavigationController's
pushViewController(_:animated:)
you use UIViewController'spresent(_:animated:completion:)
method. Note: Presented view controllers are also referred to as modal view controllers and can be used WITHOUT a UINavigationController. - visibleViewController could be the same as topViewController OR presentedViewController. It would be the same as topViewController if you pushed onto the UINavigationController last. It would be the same as the presentedViewController if you presented on a UIViewController last.
- 将 UIViewController viewA 推送到 UINavigationController.
- 让 viewA 在自身之上呈现 UIViewController viewB.
- viewA 是 topViewController.
- viewB 是被呈现的ViewController.
- viewB 也是可见的ViewController.
- 关闭模态viewB.
- viewA 现在是 topViewController 和 visibleViewController.(没有presentedViewController.)
- 弹出viewA.
- viewA 不再是 visibleViewController 或 topViewController.
- Push UIViewController viewA onto a UINavigationController.
- Have viewA present UIViewController viewB over itself.
- viewA is topViewController.
- viewB is presentedViewController.
- viewB is also visibleViewController.
- Dismiss modal viewB.
- viewA is now topViewController AND visibleViewController. (There is no presentedViewController.)
- Pop viewA.
- viewA is no longer visibleViewController OR topViewController.
示例:
总的来说,visibleViewController 似乎更有用,因为它会告诉你当前正在显示什么视图,而不管它是被推送还是呈现.
In general it seems like visibleViewController is more useful since it will tell you what view is currently showing regardless of if it was pushed or presented.
这篇关于在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!