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

我有以下代码以编程方式创建UIWebView并在其上创建UIButton以关闭它.创建没问题,但问题是我无法再参考创建的UIWebView来关闭按钮!
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(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 {
????
}

感谢您提前的帮助:)

解决方法

在ViewController.m中
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,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,40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[webView addSubview:button];





- (IBAction)close:(id)sender {

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


}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...