Clicking a link in UIWebView pushes onto the NavigationView stack

11,155

This question has been asked many times before. Please have a look at this page, it's almost exactly what you need, just change the [BrowserViewController openBrowserWithUrl:url]; to whatever you need to push your new ViewController to the navigation stack. It might look like that:

LatestView *latestView = [[LatestView alloc] initWithNibName:@"LatestView" bundle:nil]; 

//Pass the URL here, depends on how you passed it into the WebView in the first place   

// Add the ViewController to the stack
[self.navigationController pushViewController:latestView animated:YES]; 
[latestView release];   
Share:
11,155
SalsaSalsa
Author by

SalsaSalsa

Updated on June 04, 2022

Comments

  • SalsaSalsa
    SalsaSalsa almost 2 years

    Basically, I'd like to know how to intercept a click in a webview and then have a new view pop up that has a navigation bar at the top (with a back button) and the content to be the link I clicked.

    I currently have a tab bar template with 5 tabs and each tab is currently set to NavigationView and inside each of those tabs are views that contain a UIWebView. This is how I handle links:

    -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request   navigationType:(UIWebViewNavigationType)navigationType {
    
        NSURL *url = request.URL;
        NSString *urlString = url.absoluteString;
        NSRange page = [ urlString rangeOfString: @"/?page=" ];
        // URL is main page
        if ( [ urlString isEqualToString: @"http://somelink-yadayadayada.com/" ] ) {
            return YES;
        }
        // URL contains page number
        else if ( page.location != NSNotFound ) {
            return YES;
        }
        // URL is clicked link
        else {
            // THIS IS WHERE I NEED TO HAVE THE LINK OPEN THE NEW NAV VIEW.
            return NO;
        }
    }
    

    Any help would be greatly appreciated and If i need to provide more context I'll be happy to do so. Thanks.

  • SalsaSalsa
    SalsaSalsa over 13 years
    Philibbo - Thank you for the info but I guess I'm not understanding the concept. I'm in a navigation view that contains a view that contains a webview. I want to click a link that opens a new view in the navigation stack. How do I do this? Maybe I'm not grasping the conceptual process in which I should be doing this. :(
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Do you, in general, know how to add a new view to the stack of the UINavigationController?
  • SalsaSalsa
    SalsaSalsa over 13 years
    I guess not. If it's not to much trouble do you mind explaining?
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Allright. First, you are talking about a NavigationView and a WebView inside a View. To make it clear, the supposed structure would be: UINavigationController contains UIViewController contains the WebView, at least as far as I know. Then, the ViewController containing the WebView should be made the WebViews delegate. Now you can insert the code I given you in the first place. For the rest, I will edit the answer right now.
  • SalsaSalsa
    SalsaSalsa over 13 years
    A tab bar controller that has 5 tabs, each of these tabs are set to a navigation controller. Each of the navigation controllers contains a view that contains a webView.
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Please use the proper names of the classes. So you say, you have a UIView controlled by a UINavigationController? Are you sure? I'm not extremely familiar with that, but I thought a UINavigationController may only contain UIViewControllers.
  • SalsaSalsa
    SalsaSalsa over 13 years
    With proper names: UITabBarController contains 5 UINavigationControllers. Each of these UINavCon's are linked to NIB files that contain a UIView which contains a UIWebView.
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Sorry, I'm baffled. How exactly did you manage insert a UIView into a NavigationController? I'm almost certain, that we are not talking about the same thing.
  • SalsaSalsa
    SalsaSalsa over 13 years
    Each NavigationController links to a NIB file that contains a UIView and a UIWebView. Here is a picture for reference: img707.imageshack.us/img707/348/screenshot20100928at240.png
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Interesting... Tell me, what of what class is the Files Owner of the LatestView?
  • SalsaSalsa
    SalsaSalsa over 13 years
    The class that is assigned to LatestView's File owner is LatestView.m
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    Well, yes, and it's superclass?
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    In the LatestView.h, it says something like "LatestView : UI..."
  • SalsaSalsa
    SalsaSalsa over 13 years
    @interface LatestView : UIViewController <UIWebViewDelegate> {
  • Philipp Schlösser
    Philipp Schlösser over 13 years
    UIViewController and not UIView, that's what I implied 10 comments ago :) Anyway, that completes my answer, I guess. I'll just update it according to your classnames. What's more: You shouldn't call a Class "...View" when it's actually a ViewController, that would have saved us quite some time anyway.
  • SalsaSalsa
    SalsaSalsa over 13 years
    Thank you SO MUCH. That worked. I'll take the advice about naming a class View (I was thinking of each tab as the specific thing you are currently "viewing")
  • iOS Monster
    iOS Monster over 11 years
    Hi, I am facing exactly same problem. I tried this solution but it crashes my app. I have already spent 2 days looking for a solution but situation is still the same. Please help me....