我创建一个 webview 并使用同一 ViewController 中另一个 View 上的按钮加载它。 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 selected 选择器发送到实例 0x78327c0'
- (IBAction)close:(id)sender {[self.GlobeWebView removeFromSuperview];}
最佳答案
关注UIWebViewDelegate协议(protocol),您可以等待页面加载,也可以在添加 webView 本身后将按钮添加为 webView subview 。在此“关闭”按钮操作中,您需要在取消 Web vie 加载后从 super View 中删除按钮和 Web View 。
关于ios - 如何给UIwebview添加关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11506027/