[英]How to open a link from one UIWebView to another UIWebView?
我正在構建一個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;
}
}
}
您只需要通過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;
}
最后,使用該請求將Web視圖加載到NextViewController中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.