Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...