How to get UIViewController of a UIView's superview in iOS?

前端 未结 3 747
执念已碎
执念已碎 2020-12-04 16:41

I have a UIViewController in which I have a UITextView added from interface builder. Now I want to push a view when I click on hyperlink or phone number. I am able to detect

相关标签:
3条回答
  • 2020-12-04 17:21

    You can't access it directly, but you can find the next view controller (if any) by traversing the responder chain.

    This is how the Three20 framework does it:

    - (UIViewController*)viewController
    {
        for (UIView* next = [self superview]; next; next = next.superview)
        {
            UIResponder* nextResponder = [next nextResponder];
    
            if ([nextResponder isKindOfClass:[UIViewController class]])
            {
                return (UIViewController*)nextResponder;
            }
        }
    
        return nil;
    }
    
    0 讨论(0)
  • 2020-12-04 17:40

    Please note that -webView:decidePolicyForNavigationAction:... is an undocumented method (for iPhoneOS anyway. It's documented for Mac OS X) and the app will likely be rejected if that's for AppStore.


    A view controller is not associated with a view. Only reverse applies. To access a view controller, make it a globally accessible variable or property.

    If interface builder is used usually one could define an outlet to the application delegate that connects to the navigation view controller. Then you can use

    MyAppDelegate* del = [UIApplication sharedApplication].delegate;
    [del.the_navigation_view_controller pushViewController:...];
    
    0 讨论(0)
  • 2020-12-04 17:45

    You can get the UIviewController from UIView

    UIViewController *viewC = (UIViewController *)view->_viewDelegate;
    
    0 讨论(0)
提交回复
热议问题