How to close a programmed UIWebView?

I have the following code to create a UIWebView programmatically and create a UIButton on top of it to close it. The creation is fine, but the problem is that I cannot return to the created UIWebView to close it with a 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 :)

+8
xcode uibutton uiwebview
source share
4 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]; } 
+14
source share

Keep your webView as ivar of your UIViewController.

Now you have it as a local variable for a method, so you cannot reference it outside this method. Instead, declare it in the interface section of your .h file, and you can access it from all your class methods.

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

0
source share

A web view is a button supervisor, so you can 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]; } 
0
source share
 -(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; } 
-one
source share

All Articles