views:

49

answers:

1

When a user clicks a link in the present UIWebView a new view is pushed onto the navigationController's stack that contains a UIWebView. I'd like to pass the URL that was touched to this new UIWebView. How do I go about doing this?

+1  A: 

In your UIWebView delegate:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if( navigationType == UIWebViewNavigationTypeLinkClicked ) {
        YourWebViewController* vc = [[YourWebViewController alloc] initWithURL:[request URL]];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        return NO;
    }
    return YES;
}

Then you just need to implement the initializer in your custom viewcontroller:

- (id)initWithURL:(NSURL*)url {
    self = [super init];
    if( self ) {
        myURL = [url retain];
    }
    return self;
}

Then load it at an appropriate time, like viewDidAppear:

- (void)viewDidAppear:(BOOL)animated {
    [webView loadRequest:[NSURLRequest requestWithURL:myURL]];
}
aegzorz
+1 except I'm out of votes right now :)
Nimrod
Thanks, worked perfectly.
Salsa