如何取消一个UIWebView?

我模式展示一个UIWebView作为其视图的UIViewController 。 当用户点击取消button时,如何解除UIWebView

我有一个想法是有取消button链接到http://取消 ,然后登记

 - (void)webViewDidStartLoad:(UIWebView *)webView 

如果webView.request.URL.host isEqualToString:@"cancel" ,则closures视图控制器。

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

你走在正确的道路上,但是你的方法稍微偏离了一点。 您不需要或希望这是一个HTTP URL。 cancel:您的urlcancel:

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

然后在web视图委托中实现webView:shouldStartLoadWithRequest:navigationType: 像这样(未经testing):

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

如果你想停止加载一个Web视图或只是closures包含它的模式视图控制器,这并不完全清楚。

停止加载:

 [webView stopLoading]; 

closures视图控制器:

 [self dismissModalViewControllerAnimated:YES]; 

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