开发者

Passing a URL from one UIWebView to another UIWebview

开发者 https://www.devze.com 2023-01-20 07:44 出处:网络
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 UIWebVie

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

精彩评论

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

关注公众号