我有一个功能,即在全屏模式下在WebView中打开PDF文件。
如何添加“完成”按钮,可以关闭WebView?
- (IBAction)openPDF:(id)sender {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
webView.contentMode = UIViewContentModeScaleAspectFit;
NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
CGRect frame = webView.frame;
webView.scalesPageToFit = YES;
webView.frame = frame;
[webView loadRequest:request];
[self.view addSubview:webView];
}
答案 0 :(得分:0)
使UIWebView * webView全局,
@property(nonatomic, strong) UIWebView *webView;
然后在 - (IBAction)openPDF:(id)发件人方法中初始化该webview;
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
在视图和- (IBAction)doneButtonPressed:(id)sender
方法
从视图中删除webview;
[_webview removeFromSuperview];
_webView = nil;
答案 1 :(得分:-1)
我要做的是,创建一个新的 UIViewController 子类,比如带有nib的WebViewController。然后我会添加 UINavigationBar ,其中包含关闭按钮和 UIWebView 。 现在做类似的事情:
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.loadURL = [NSURL URLWithString:@"http://www.google.com"];
[self presentModalViewController:webViewController animated:YES];
[webViewController release]; //If you use ARC, the no need for this line
在 WebViewController 中定义:
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSURL *loadURL;
- (IBAction)close:(id)sender;
实施:
- (void)viewDidLoad {
[super viewDidLoad]
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.loadURL];
[self.webView loadRequest:urlRequest];
}
- (IBAction)close:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}