UIwebview图片到新的视图控制器

UIwebview picture to new view controller

本文关键字:视图 控制器 UIwebview      更新时间:2024-01-17

我有一个UIWebview,显示带有图片链接的html页面。当用户单击链接时,我如何才能打开一个新的视图控制器来显示html图像,并可以选择返回到上一个屏幕?

如果你确定你的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