UIwebview图片到新视图控制器

时间:2015-02-18 06:24:12

标签: javascript ios objective-c xcode

我有一个UIWebview显示带有图片链接的html页面。如何在用户点击链接时实现这一点,它会显示一个显示html图像的新视图控制器,并可以选择返回上一个屏幕?

1 个答案:

答案 0 :(得分:0)

如果您确定您的Web视图仅包含图像链接,那么您可以使当前视图控制器实现UIWebViewDelegate,尤其是:

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType {
     // Here you initialized your new view controller from your storyboard and pass the image url to it, and it shall know how to load it in an image view or anything, and you return NO, to stop the web view from loading the request so that the user only sees your links
    SecondViewController* second = [self.storyboard instantiateViewControllerWithIdentifier:@"secondController"];
    second.urlRequest = request;

    [self.navigationController pushViewController:second animated:YES];

    return NO;
}

当然上面的代码假定了很多东西:

你的SecondViewController类中有一个名为urlRequest的属性。

您有一个包含FirstViewController的导航控制器。

您正在使用故事板,而您的SecondViewController ID是secondController