xcode – 如何关闭以编程方式创建的UIWebView?

前端之家收集整理的这篇文章主要介绍了xcode – 如何关闭以编程方式创建的UIWebView?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码以编程方式创建UIWebView并在其上创建UIButton以关闭它.创建没问题,但问题是我无法再参考创建的UIWebView来关闭按钮!
  1. UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,320,480)];
  2. NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  3. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  4. [webView loadRequest:requestObj];
  5. [self.view addSubview:webView];
  6.  
  7. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  8. [button addTarget:self
  9. action:@selector(aMethod:)
  10. forControlEvents:UIControlEventTouchDown];
  11. [button setTitle:@"Close" forState:UIControlStateNormal];
  12. button.frame = CGRectMake(80,210,160,40);
  13. [button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
  14. [webView addSubview:button];
  15.  
  16. - (IBAction)close:(id)sender {
  17. ????
  18. }

感谢您提前的帮助:)

解决方法

在ViewController.m中
  1. UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,480)];
  2.  
  3. // tag will be used to get this webview later
  4. webView.tag=55;
  5. NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
  6. NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  7. [webView loadRequest:requestObj];
  8. [self.view addSubview:webView];
  9.  
  10. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  11. [button addTarget:self
  12. action:@selector(close:)
  13. forControlEvents:UIControlEventTouchDown];
  14. [button setTitle:@"Close" forState:UIControlStateNormal];
  15. button.frame = CGRectMake(80,40);
  16. [button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
  17. [webView addSubview:button];
  18.  
  19.  
  20.  
  21.  
  22.  
  23. - (IBAction)close:(id)sender {
  24.  
  25. [[self.view viewWithTag:55] removeFromSuperview];
  26.  
  27.  
  28. }

猜你在找的iOS相关文章