ios - 如何打开从一个 UIWebView 到另一个 UIWebView 的链接?

标签 ios objective-c webview uiscrollview

我正在构建一个 iOS 应用程序。
我的 UIWebView 中包含带有超链接的 HTML 内容,但我无法打开指向另一个 UIWebView 的链接。
我使用 UIWebView 作为 ViewController 的 subview 。这是代码:

- (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 View 连接到一个新 Controller ,并将请求传递给它即可。所以在你的第一个例子中是这样的,

 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 参数是委托(delegate)方法返回的请求。将此请求传递给您要访问的 View Controller 中的属性,

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSURLRequest *)sender {
    NextViewController *next = segue.destinationViewController;
    next.request = sender;
}

最后,使用该请求在 NextViewController 中加载 Web View 。

关于ios - 如何打开从一个 UIWebView 到另一个 UIWebView 的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24635107/

相关文章:

objective-c - objective-c - 核心数据保存方法

objective-c - 如何更改水平 UITableView 的反弹区域的颜色/背景

android - 加载时在 Android WebView 中聚焦输入元素并显示键盘

Android:在其他 Activity 的 WebView 上加载 url

ios - 如果在 ios 6 中删除了文档文件夹,如何重新创建它?

ios - iPad Retina Display 特定的媒体查询

ios - 如何移动 uitableview 自定义单元格?

ios - 如何仅更改一个 View 的 navigationTitle 颜色?

ios - 在 UINavigationController Stack 中支持一个 UIViewController 横向和其他 UIViewController 纵向

android - TextView 中的多个 URL