Given a view, how do I get its viewController?

后端 未结 13 766
北海茫月
北海茫月 2020-11-28 02:00

I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the

13条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 02:46

    For debug purposes only, you can call _viewDelegate on views to get their view controllers. This is private API, so not safe for App Store, but for debugging it is useful.

    Other useful methods:

    • _viewControllerForAncestor - get the first controller that manages a view in the superview chain. (thanks n00neimp0rtant)
    • _rootAncestorViewController - get the ancestor controller whose view hierarchy is set in the window currently.

提交回复
热议问题