我有以下代码以编程方式创建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]; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。