我创建一个webview并使用同一个ViewController上的另一个视图上的按钮加载它。 webview从Doc dir加载pdf文件。目前为止一切正常。但现在用户应该有一个按钮来关闭web视图。我该怎么做呢?如何将关闭按钮添加到UIwebview
谢谢你的回答!
编辑:现在我设法添加关闭按钮。看到below.but如果我试图关闭
这里是我的代码,创建web视图更新的代码:
- (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方法:]:无法识别的选择发送到实例0x78327c0'
- (IBAction为)密切:(ID)发送方{[self.GlobeWebView removeFromSuperview];}
谢谢!我添加了按钮,但是如果点击它,我会收到一个错误消息。看到上面的代码编辑! – Jan 2012-07-16 14:26:42
@Jan,因为您将方法命名为close并为'aMethod'添加了选择器,正确的方法是[button addTarget:self action:@selector(close :) forControlEvents:UIControlEventTouchDown]; – 2012-07-16 14:29:30
谢谢你的提示。我改变了它,但仍然收到上述错误!你有什么主意吗? – Jan 2012-07-16 14:42:55