Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close UIWebView created programmatically?

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 :)

like image 668
DeZigny Avatar asked Jul 14 '11 12:07

DeZigny


1 Answers

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


}
like image 56
Vijay-Apple-Dev.blogspot.com Avatar answered Nov 08 '22 19:11

Vijay-Apple-Dev.blogspot.com