xcode 单击 UIWebView 上的链接时打开新的 UIViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10959456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a new UIViewController when a link on a UIWebView is clicked
提问by CodeRed
I found this thread which matches my problem: Clicking a link in UIWebView pushes onto the NavigationView stack
我发现这个线程符合我的问题:单击 UIWebView 中的链接会推送到 NavigationView 堆栈上
However, the following situations are different: Instead of using a navigationController, I am using a View Based application to manually switch to different viewcontroller when corresponding buttons are pressed. Rather than using links I am using onClick method to detect when a user clicks on something on a UIWebView and trying to open a new ViewController with a new UIWebView.
但是,以下情况有所不同: 我没有使用导航控制器,而是使用基于视图的应用程序在按下相应按钮时手动切换到不同的视图控制器。我没有使用链接,而是使用 onClick 方法来检测用户何时单击 UIWebView 上的某些内容并尝试使用新的 UIWebView 打开新的 ViewController。
Will the same code work or do i have to make some changes?
相同的代码会起作用还是我必须进行一些更改?
回答by Omar Abdelhafith
You will have to do some modifications
你将不得不做一些修改
1st:
when onClick
, you will have to redirect your current page to a new location
This location for example will be MYLocation://GoThere
第一:当onClick
,您必须将当前页面重定向到新位置,例如此位置将是MYLocation://GoThere
2nd:
Update the shouldStartLoadWithRequest
第二:更新 shouldStartLoadWithRequest
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Check if special link
if ( [ urlString isEqualToString: @"MYLocation://GoThere" ] ) {
//Here present the new view controller
MyViewController *controller = [[MyViewController alloc] init];
[self presentViewController:controller animated:YES];
return NO;
}
return YES;
}
回答by isolMAC
I think you may use the shouldStartLoadWithRequest as Omar posted. If the new viewcontroller has a webviewcontroller, then you should pass the url (or even the request) to the new viewcontroller to show the clicked URL.
我认为您可以像 Omar 发布的那样使用 shouldStartLoadWithRequest。如果新的 viewcontroller 有一个 webviewcontroller,那么你应该将 url(甚至是请求)传递给新的 viewcontroller 以显示点击的 URL。