views:

789

answers:

2

I have a UIViewController with a UIWebView (webview1) in it. The webview is only a few lines of text but does have a link in it. Rather than open the link, which goes to an external website, in this tiny space, I'd like to send it on to webview2, which will be a full screen. The goal is to keep the web requests within my app rather than Safari. Instead of creating another controller for webview2, I'd like to use webview1's controller.

In Webview1Controller controller, I do this in webViewLoad:

webview1.delegate = self;

Here's where I hand off the web request to webview2, which works fine:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

myapp *delegate = [[UIApplication sharedApplication] delegate];
Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:@"webview2" bundle:nil];
//self.view = webview2;'
[delegate.navigationController pushViewController: webview1Controller animated:YES];
[webview1Controller.webview2 loadRequest:request];
[webview1Controller release];
return YES;
}

In Interface Builder, I have webview2.xib File's Owner class set to Webview1Controller. It's "view" and the webview2outlet are connected. I have an IBOutlet in Webview1Controller named webview2outlet.

When i go back to webview1, it has also loaded the same request. Is there a way to stop webview1 from loading anything? If I return NO in the above method, webview1 won't render my content.

One solution is just to reload webview1 content on viewWillAppear, which works. But is there a better way?

+1  A: 

Return NO from the delegate method.

Regarding your comment, I think what you want to do is make your delegate method check which webview is calling your controller:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if (webView == webview1) {
        Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:@"webview2" bundle:nil];
        [self.navigationController pushViewController:webview1Controller animated:YES];
        [webview1Controller.webview2 loadRequest:request];
        [webview1Controller release];
        return NO;
    }
    else {
        return YES;
    }
}

(Note, also, that UIViewController has a navigationController property so you can use that rather than getting it through your app delegate).

Daniel Dickison
See comment below.
4thSpace
See edited answer -- it's a bit long to put in a comment.
Daniel Dickison
Perfect. Thanks.
4thSpace
A: 

What happens if you load a copy of the request instead of the original, and then return NO?

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    myapp *delegate = [[UIApplication sharedApplication] delegate];
    Webview1Controller *webview1Controller = [[Webview1Controller alloc] initWithNibName:@"webview2" bundle:nil];
    //self.view = webview2;'
    [delegate.navigationController pushViewController: webview1Controller animated:YES];
    [webview1Controller.webview2 loadRequest:[[request copy] autorelease]];
    [webview1Controller release];
    return NO;
}
Chris Lundie
Returning NO causes webview1 to be blank. When the main view loads, the above method also fires. Since it results in a blank webview1, there will never be any links to click.
4thSpace