单击UIWebView上的链接时打开新的UIViewController [英] Opening a new UIViewController when a link on a UIWebView is clicked
问题描述
我发现这个帖子符合我的问题:单击UIWebView中的链接会推送到NavigationView堆栈
I found this thread which matches my problem: Clicking a link in UIWebView pushes onto the NavigationView stack
但是,以下情况有所不同:
我使用的不是导航控制器,而是使用一个基于视图的应用程序,当按下相应的按钮时手动切换到不同的viewcontroller。
而不是使用链接我使用onClick方法来检测用户何时点击UIWebView上的某些内容并尝试使用新的UIWebView打开新的ViewController。
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.
相同的代码是否有效或是否需要进行一些更改?
Will the same code work or do i have to make some changes?
推荐答案
您将需要做一些修改
1st:
当 onClick
时,您必须将当前页面重定向到新位置
例如,这个位置将是 MYLocation:// GoThere
1st:
when onClick
, you will have to redirect your current page to a new location
This location for example will be MYLocation://GoThere
第二名:
更新 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;
}
这篇关于单击UIWebView上的链接时打开新的UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!