在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 有什么区别? [英] In UINavigationController what is the difference between topViewController, visibleViewController, presentedViewController?

查看:16
本文介绍了在 UINavigationController 中,topViewController、visibleViewController、presentedViewController 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UINavigationController 有 3 个控制器,它们听起来都与我非常相似:topViewControllervisibleViewControllerpresentedViewController.

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's present(_: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's present(_: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.
    • 示例:

      1. 将 UIViewController viewA 推送到 UINavigationController.
      2. viewA 在自身之上呈现 UIViewController viewB.
      3. viewA 是 topViewController.
      4. viewB 是被呈现的ViewController.
      5. viewB 也是可见的ViewController.
      6. 关闭模态viewB.
      7. viewA 现在是 topViewController 和 visibleViewController.(没有presentedViewController.)
      8. 弹出viewA.
      9. viewA 不再是 visibleViewController 或 topViewController.
      1. Push UIViewController viewA onto a UINavigationController.
      2. Have viewA present UIViewController viewB over itself.
      3. viewA is topViewController.
      4. viewB is presentedViewController.
      5. viewB is also visibleViewController.
      6. Dismiss modal viewB.
      7. viewA is now topViewController AND visibleViewController. (There is no presentedViewController.)
      8. Pop viewA.
      9. 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 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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