开发者

Passing a URL from one UIWebView to another UIWebview

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 开发者_StackOverflowdoing this?


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]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜