如何在 iOS 中获取 UIView 的超级视图的 UIViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2309569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:56:56  来源:igfitidea点击:

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

iosobjective-cuiviewuiviewcontrolleriphone-sdk-3.0

提问by Rahul Vyas

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 that which url is clicked using a method I found in stackoverflow. Here is the method

我有一个 UIViewController,其中我有一个从界面构建器添加的 UITextView。现在我想在点击超链接或电话号码时推送一个视图。我能够使用我在 stackoverflow 中找到的方法检测点击了哪个 url。这是方法

@interface UITextView (Override)
@end

@class WebView, WebFrame;
@protocol WebPolicyDecisionListener;

@implementation UITextView (Override)

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{
    NSLog(@"request: %@", request);
}
@end

Now I want to get the viewController of the textview's superview so that I can push another viewController when I click on URL/Phone Number.

现在我想获取 textview 的超级视图的 viewController 以便我可以在单击 URL/电话号码时推送另一个 viewController。

回答by Felixyz

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 frameworkdoes it:

这是怎样的Three20框架做的:

- (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;
}

回答by kennytm

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.

请注意,这-webView:decidePolicyForNavigationAction:...是一种未记录的方法(无论如何,对于 iPhoneOS。它已记录在 Mac OS X 中),如果用于 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:...];

回答by Prabhjot Singh Gogana

You can get the UIviewController from UIView

您可以从 UIView 获取 UIviewController

UIViewController *viewC = (UIViewController *)view->_viewDelegate;