开发者

How to close UIWebView created programmatically?

开发者 https://www.devze.com 2023-03-20 02:18 出处:网络
I have the following code to create UIWebView programmatically and create UIButton on top of it to close it. The creation is OK, but the problem I can\'t refer back to the created UIWebView to close i

I have the following code to create UIWebView programmatically and create UIButton on top of it to close it. The creation is OK, but the problem I can't refer back to the created UIWebView to close it from the button!

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
NSURL *url = [NSURL URLWithString:@"http://www.google.开发者_运维百科com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 210, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];

- (IBAction)close:(id)sender {
????
}

Thanks for your help in advance :)


In ViewController.m

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

// tag will be used to get this webview later
webView.tag=55;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(close:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Close" forState:UIControlStateNormal];
button.frame = CGRectMake(80, 210, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];





- (IBAction)close:(id)sender {

    [[self.view viewWithTag:55] removeFromSuperview];


}


Keep your webView as an ivar of your UIViewController.

Right now you have it as a local variable to the method so you can't refer to it outside that method. Instead, declare it in your interface section of your .h file and you'll be able to access it from all your class methods.

And don't forget to release it in your dealloc method!


The web view is the button's superview so you should be able to get it like this,

UIWebView * webView = [(UIButton *)sender superview];

Now you can do removeFromSuperview or something else to make it disappear.

Edit

- (IBAction)close:(id)sender {
    UIWebView * webView = [(UIButton *)sender superview];
    [webView removeFromSuperview];
}


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

    NSLog(@"%@",request.URL.absoluteString);
    NSString *absoluteUrl = [[request URL] absoluteString];
    if ([absoluteUrl isEqualToString:@"https://google.com/login?"]) {
        NSLog(@"move to another view or close the web view");
        [portfolioWebView removeFromSuperview];

        return NO;
    }

    return YES;
}
0

精彩评论

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