2012-07-16 87 views
0

我創建一個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];}

回答

1

Followint UIWebViewDelegate協議,您可以等待直到加載頁面,或者在添加webView後添加按鈕作爲webView子視圖。在這個'關閉'按鈕操作中,您需要在取消Web vie加載後從超級視圖中刪除按鈕和Web視圖。

+0

謝謝!我添加了按鈕,但是如果點擊它,我會收到一個錯誤消息。看到上面的代碼編輯! – Jan 2012-07-16 14:26:42

+0

@Jan,因爲您將方法命名爲close併爲'aMethod'添加了選擇器,正確的方法是[button addTarget:self action:@selector(close :) forControlEvents:UIControlEventTouchDown]; – 2012-07-16 14:29:30

+0

謝謝你的提示。我改變了它,但仍然收到上述錯誤!你有什麼主意嗎? – Jan 2012-07-16 14:42:55

0

您的a方法:有什麼參數嗎?如果不在選擇器中清除