我正在以编程方式创建 UIWebView,但我需要触发此函数,以便在自定义 View 中打开 href:
-(bool) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//You might need to set up a interceptLinks-Bool since you don't want to intercept the initial loading of the content
if (interceptLinks) {
NSURL *url = request.URL;
//This launches your custom ViewController, replace it with your initialization-code
BrowserViewController *bv = [[BrowserViewController alloc] initWithUrl:url];
bv.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:bv animated:YES];
[bv release];
return NO;
}
//No need to intercept the initial request to fill the WebView
else {
interceptLinks = YES;
return YES;
}
}
如何将委托(delegate)链接到以编程方式创建的 UIWebView 以便函数触发?
最佳答案
您在上面所做的看起来是正确的。
只需在接口(interface)类和实现类上添加 UIWebViewDelegate 协议(protocol)
添加
webview.delegate = self;
所以你的 shouldStartLoadWithRequest 被调用。
关于objective-c - 使用委托(delegate)以编程方式创建 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489369/