Given a view, how do I get its viewController?

后端 未结 13 745
北海茫月
北海茫月 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

    If you are not familiar with the code and you want to find ViewController coresponding to given view, then you can try:

    1. Run app in debug
    2. Navigate to screen
    3. Start View inspector
    4. Grab the View you want to find (or a child view even better)
    5. From the right pane get the address (e.g. 0x7fe523bd3000)
    6. In debug console start writing commands:
        po (UIView *)0x7fe523bd3000
        po [(UIView *)0x7fe523bd3000 nextResponder]
        po [[(UIView *)0x7fe523bd3000 nextResponder] nextResponder]
        po [[[(UIView *)0x7fe523bd3000 nextResponder] nextResponder] nextResponder]
        ...
    

    In most cases you will get UIView, but from time to time there will be UIViewController based class.

提交回复
热议问题