Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In UINavigationController what is the difference between 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?

like image 555
TenaciousJay Avatar asked Oct 28 '15 15:10

TenaciousJay


People also ask

What is uinavigationcontroller UIViewController?

@MainActor class UINavigationController : UIViewController A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.

What is the difference between a view controller and navigation controller?

The navigation controller pushes the specified view controller onto its navigation stack. The navigation controller presents the specified view controller modally. The behaviors of other segue types are unchanged. A navigation controller supports the following behaviors for its interface:

What happens to toolbar items when the active view controller changes?

When the active view controller changes, the navigation controller updates the toolbar items to match the new view controller, animating the new items into position when appropriate.

How do I remove the top view controller from the interface?

Tapping the back button in the navigation bar at the top of the interface removes the top view controller, thereby revealing the view controller underneath. Use a navigation interface to mimic the organization of hierarchical data managed by your app.


2 Answers

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

like image 117
TenaciousJay Avatar answered Oct 16 '22 16:10

TenaciousJay


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

like image 36
Cehm Avatar answered Oct 16 '22 16:10

Cehm