如何在Objective c中显示来自Web视图的弹出窗口

时间:2013-09-04 09:31:19

标签: ios objective-c uiwebview

我从网址获取数据。我在Web视图中显示一切都很好,但我的要求是我想添加像维基百科这样的参考功能,如图所示。在维基百科中点击该标签时,它向下滚动到该位置,但现在我的要求是当用户点击该引用时我想显示弹出窗口。可能吗?

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以使用 UIWebViewDelegate 方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

这将在Web视图开始加载之前调用。

此处参数UIWebViewNavigationType是一个枚举,它保存导航类型的值,检查navigationType是否为UIWebViewNavigationTypeLinkClicked。如果是,则表示webView正在加载页面,因为单击了任何链接。

代码看起来像这样

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"url" message:[[request URL] absoluteString] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    return YES;
}

希望这会对你有所帮助。