如何向WebView添加导航按钮?

时间:2013-05-01 09:40:25

标签: ios objective-c xcode webview

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

2 个答案:

答案 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];
}