Opening a new UIViewController when a link on a UI

2020-02-24 04:41发布

I found this thread which matches my problem: Clicking a link in UIWebView pushes onto the NavigationView stack

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?

2条回答
唯我独甜
2楼-- · 2020-02-24 05:17

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

2nd: Update the 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;

}
查看更多
甜甜的少女心
3楼-- · 2020-02-24 05:19

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.

查看更多
登录 后发表回答