0

我有一个功能,可以在全屏模式下在 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];
}
4

2 回答 2

0

使 UIWebView *webView 全局化,

@property(nonatomic, strong) UIWebView *webView;

然后在里面初始化那个webview - (IBAction)openPDF:(id)sender方法;

_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];


将 DONE 按钮添加到您的视图中,并在- (IBAction)doneButtonPressed:(id)sender方法中

从视图中删除 webview;

[_webview removeFromSuperview];
_webView = nil;
于 2013-05-01T09:44:15.117 回答
-1

我要做的是,创建一个新的UIViewController子类,比如带有笔尖的 WebViewController 。然后我会添加一个带有关闭按钮UIWebView的UINavigationBar。现在做类似的事情:

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];
}
于 2013-05-01T09:47:37.007 回答