In UINavigationController what is the difference b

2019-03-14 20:30发布

问题:

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?

回答1:

  • 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.

Example:

  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.

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.



回答2:

presentedViewController is the current modal presented on screen. topViewController is view controller on top of the navigation stack (see viewControllers() method) and visibleViewController is the currently displayed view controller on screen (can be either a controller, a modal, a UINavigationController, a UITabbarController, etc).



回答3:

  • TopViewController is the topmost VC in the navigation stack. OR the View controller which was pushed last.

  • PresentedViewController is the viewController presented modally from the navigationController. It is not part of NaivagationController's horizontal stack, but a viewController presented over it.

  • Of these two viewControllers, the one that is visible to the user will be the visibleViewController of the navigationController.