如何在 iOS 中获取 UIView 的超级视图的 UIViewController? [英] How to get UIViewController of a UIView's superview in iOS?

查看:33
本文介绍了如何在 iOS 中获取 UIView 的超级视图的 UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

@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

现在我想获取textview的superview的viewController,这样当我点击URL/电话号码时,我可以推送另一个viewController.

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.

推荐答案

不能直接访问,但可以通过遍历响应者链找到下一个视图控制器(如果有).

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

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

这篇关于如何在 iOS 中获取 UIView 的超级视图的 UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆