我正在构建一个iOS应用程序
我在UIWebView
中包含带有超链接的HTML内容,但我无法打开指向其他UIWebView
的链接。
我使用UIWebView
作为ViewController
的子视图。这是代码:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
//write the handling code here.
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
// some other typical parameters within a UIWebView. Use what is needed
case UIWebViewNavigationTypeFormResubmitted: return YES;
case UIWebViewNavigationTypeReload: return YES;
//for all other cases, including UIWebViewNavigationTypeOther called when UIWebView is loading for the first time
default: {
if (!isWebViewLoaded) {
isWebViewLoaded = true;
return YES;
}
else
return NO;
}
}
}
答案 0 :(得分:4)
您只需要通过Web视图转到新控制器,然后将请求传递给它。在第一种情况下就是这样的事情,
case UIWebViewNavigationTypeLinkClicked: { //this is when a user tap is detected
[self performSegueWithIdentifier:@"Next" sender:request];
isWebViewLoaded = false; //set this to false only if you open another view controller.
return NO; //prevent tapped URL from loading inside the UIWebView.
}
请注意,performSegueWithIdentifier:sender:中的sender参数是委托方法返回的请求。将此请求传递给您要转到的视图控制器中的属性
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
NextViewController *next = segue.destinationViewController;
next.request = sender;
}
最后,使用该请求在NextViewController中加载Web视图。