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

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

问题描述

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 的 pushViewController(_:animated:) 方法推送到 UINavigationController.将以前的控制器推开并替换它.
  • presentedViewController - 呈现在另一个之上的视图控制器(基本上它覆盖了另一个视图控制器,而不是将其推开).代替 UINavigationController's pushViewController(_:animated:) 你使用 UIViewController's present(_:animated:completion:)方法.注意:呈现的视图控制器也称为模态视图控制器,可以在没有 UINavigationController 的情况下使用.
  • visibleViewController 可以与 topViewController 或 presentViewController 相同.如果您最后推送到 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 也是 visibleViewController.
      6. 关闭模态 viewB.
      7. viewA 现在是 topViewController AND visibleViewController.(没有提供的ViewController.)
      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、presentViewController 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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