在哪里设置代表

时间:2012-10-30 23:19:31

标签: objective-c delegates

我试图覆盖UIWebView对象中的URL点击。我知道我需要在'webView委托设置'的位置添加以下代码(根据this thread):

// TODO: This should intercept URL clicks in a UIWebView, causing (for now) links to open in Safari. It doesn't work yet; possibly it's in the wrong place. Should be in the same place as we 'set the webView delegate'...
// This doesn't appear to be set anywhere yet, which is probably where I'm going wrong.
- (BOOL)webView: (UIWebView*)webView shouldStartLoadWithRequest: (NSURLRequest*)request navigationType: (UIWebViewNavigationType)navigationType {

    if ( navigationType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }

    return YES;
}

但是,我不知道在哪里实际放置代码。上述线程的一个建议是将它放在viewDidLoad方法中,在一行“self.webView.delegate = self;”下面。但是,我的UIWebViews实际上是自定义UIView的一部分,它被称为UIAlertView(事实上,它几乎是UIAlertView的精确复制品,除了它包含一个UIWebView)...所以,我的主应用程序的ViewController代码没有'看到'self.webView对象,所以我无法在那里设置委托。并且,指定我的自定义UIView对象(调用UIWebView对象)的'm'文件没有viewDidLoad方法。

真的很挣扎,可能是因为我不知道代表的位置。

1 个答案:

答案 0 :(得分:1)

  

并且,指定我的自定义UIView对象(调用UIWebView对象)的'm'文件没有viewDidLoad方法。

将其放入- initWithFrame:方法中:

- (id)initWithFrame:(CGRect)frm
{
    if ((self = [super initWithFrame:frm])) {
         webView = [[UIWebView alloc] initWithFrame:frm]; // or whatever

         self.webView.delegate = self;
    }
    return self;
}
相关问题