我创建了一个 webview 并在同一个 ViewController 的另一个视图上使用按钮加载它。webview 正在从 Doc 目录加载一个 pdf 文件。到目前为止一切正常。但是现在用户应该有一个按钮来关闭 webview。我该怎么做呢?
谢谢您的回答!
编辑:现在我设法添加了关闭按钮。请参阅下面的更新代码。但是如果我尝试关闭
这是我的代码,它创建了 webview:
- (IBAction)globeButton:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [documentsDirectory stringByAppendingPathComponent:@"files"];
NSString *fileName = [NSString stringWithFormat:@"%@/PDFfilename.pdf",
filePathAndDirectory];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 50, 900, 650)];
NSURL *targetURL = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[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(390, 605, 160, 40);
[button addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchDown];
[webView addSubview:button];}
但是下一行导致错误:-[SecondViewController aMethod:]: unrecognized selector sent to instance 0x78327c0'
- (IBAction)close:(id)sender {[self.GlobeWebView removeFromSuperview];}