如何取消一个UIWebView?

发布于 2024-11-30 09:38:21 字数 392 浏览 0 评论 0原文

我以模态方式呈现一个 UIViewController ,并使用 UIWebView 作为其视图。当用户点击取消按钮时,如何关闭 UIWebView

我的一个想法是将取消按钮链接到 http://cancel ,然后检查

- (void)webViewDidStartLoad:(UIWebView *)webView

If webView.request.URL.host isEqualToString: @"cancel",然后关闭视图控制器。

主机中是否必须有一个点,例如“cancel.com”?

I'm modally presenting a UIViewController with a UIWebView as its view. How do I dismiss the UIWebView when the user hits a cancel button on it?

One idea I have is to have the cancel button link to http://cancel and then check in

- (void)webViewDidStartLoad:(UIWebView *)webView

If webView.request.URL.host isEqualToString:@"cancel", then dismiss the view controller.

Does the host have to have a dot in it, e.g., "cancel.com"?

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

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

发布评论

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

评论(2

唠甜嗑 2024-12-07 09:38:21

你走在正确的道路上,但你的方法略有偏差。您不需要也不希望这是一个 HTTP URL。将您的 URL 设置为取消:

<a href="cancel:">Thing to click</a>

然后在 Web 视图委托中实现 webView:shouldStartLoadWithRequest:navigationType: 。像这样的东西(未经测试):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([request.URL.scheme isEqualToString:@"cancel"]) {
        [self dismissModalViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}

You're on the right path, but you're approach is slightly off. You don't need or want this to be an HTTP URL. Make your URL cancel:.

<a href="cancel:">Thing to click</a>

Then implement webView:shouldStartLoadWithRequest:navigationType: in your web view delegate. Something like this (untested):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([request.URL.scheme isEqualToString:@"cancel"]) {
        [self dismissModalViewControllerAnimated:YES];
        return NO;
    }
    return YES;
}
墨落成白 2024-12-07 09:38:21

目前尚不完全清楚您是想停止在 Web 视图中加载还是只是关闭包含它的模态视图控制器。

停止加载:

[webView stopLoading];

关闭视图控制器:

[self dismissModalViewControllerAnimated:YES];

在释放 Web 视图之前,不要忘记将其 delegate 设置为 nil。

It's not entirely clear if you want to stop loading in a web view or just dismiss the modal view controller that's containing it.

To stop loading:

[webView stopLoading];

To dismiss the view controller:

[self dismissModalViewControllerAnimated:YES];

Don't forget to set the web view's delegate to nil before you release it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文