单击另一个 Web 视图上的图像后在 Web 视图中打开图像?

发布于 2024-11-05 12:17:19 字数 108 浏览 0 评论 0 原文

我正在开发一个 iPhone 应用程序,其中有大表格可以在网络视图上显示。我计划在实际表格的位置提供一个小图像,当用户单击它时,实际图像将在另一个 webView 中打开。我正在寻找正确的方向。请帮忙。

I am developing an iPhone app in which there are big tables to show on web view. I am planning to give a small image at the place of actual table and when the user will click on that, the actual image will open in another webView. I am looking for right direction. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-11-12 12:17:19

为包含小图像的第一个 Web 视图实现 UIWebViewDelegate 协议方法 shouldStartLoadWithRequest

当用户单击您的图像时,您需要拦截此事件并决定做什么以及如何做。

shouldStartLoadWithRequest 方法。请参阅 UIWebView 文档 了解更多详细信息。 shouldStartLoadWithRequest 必须返回一个 BOOL 来指示 webview 应该继续执行用户执行的操作(返回 YES)或不继续(返回 NO)。

现在您知道已单击链接,您必须确保它是图像。这是通过评估请求参数传回的 url 来完成的。我在示例中编写了一个方法 clickedOnImageWithUrl ,如果单击图像,该方法将返回 YES 。但是,我不知道链接中嵌入的小图像是什么样子,我无法提供有关该方法的更多详细信息。如果返回 YES,您可以显示新的 webView。

然后,代码可能如下所示以拦截单击的链接:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        // check if user clicked on your image...clickedOnImageWithUrl is made up
        if([self clickedOnImageWithUrl:[aRequest url]]) {

             // push new webview with image ...

             return NO;
        }            
    }

    return YES;
}

Implement the UIWebViewDelegate protocol method shouldStartLoadWithRequest for your first webview containing your small images.

When a user clicks your image you need to intercept this event and decide what and how to do.

The shouldStartLoadWithRequest method is called when some kind of interaction is triggered in you webview, such as a link being clicked. Refer to the UIWebView documentation for more detail. The shouldStartLoadWithRequest has to return a BOOL to indicate the webview that it should proceed with the action the user performed(return YES) or not(return NO).

Now that you know that a link has been clicked you have to make sure, that it has been an image. This is done by evaluate the url passed back by the request parameter. I've made up a method clickedOnImageWithUrl in the example, which will return YES if an image has been clicked. However, I don't know what your small images embedded in a link look like I cannot provide any more detail on that method. If it returns YES you can show your new webView.

Then the code could look like this to intercept a clicked link:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        // check if user clicked on your image...clickedOnImageWithUrl is made up
        if([self clickedOnImageWithUrl:[aRequest url]]) {

             // push new webview with image ...

             return NO;
        }            
    }

    return YES;
}
拥抱影子 2024-11-12 12:17:19

@Yogi:当你要在另一个 webView 中显示图像时,为什么要在 UIWebView 之上有一个表格视图。我想只有一个 WebView 就足够了。顺便说一句,您可以通过拥有图像详细信息的字典或者甚至包含图像 url 的数组来实现您想要的功能。在UITableViewDelegate方法中,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

您可以使用indexpath将图像的url传递给webview,webview可以打开url来查看图像。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the url for the image here
NSUrlRequest *urlRequest = [NSUrlRequest requestWithURL:urImageUrl];
[urWebView loadRequest:urlRequest];
}

@Yogi: Why do you want to have a table view on top of a UIWebView when you are going to display the image in another webView. I guess just one WebView would be sufficient. By the way you can implement what you want by having a dictionary for the details of the image or perhaps even an array which contains the url for the images. And in the UITableViewDelegate method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

You can pass the url of the image using the indexpath to the webview which can open the url to view the image.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the url for the image here
NSUrlRequest *urlRequest = [NSUrlRequest requestWithURL:urImageUrl];
[urWebView loadRequest:urlRequest];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文