2009-11-23 61 views
5

通過webview,可以設置一個簡單的「返回」按鈕(無論是在導航欄還是頂部工具欄中),該按鈕不會在WebView的第一個URL上顯示後退按鈕,而且只能出現在第二個URL上以回到第一個?導航WebView

除非出現這種錯誤,否則在很多混合原生/網絡應用程序(如News應用程序)中,您經常會在表格中看到新聞文章(HTML頁面而非「xcode」中的表格)到文章詳細頁面(再次,HTML而不是本地編碼) - 我無法確定的是,詳細信息頁面(webview中的第2個URL)是如何顯示的「後退按鈕」,但表格(webview中的第1個URL)沒有按鈕顯示在這些類型的應用程序?

目前,我有一個webview的描述,在屏幕頂部的工具欄中有一個'後退'欄按鈕項目(出口作爲'cangoback'爲WebView),但按鈕是從一開始就可見的,當沒有以「回去」頁面 -

我已經有了簡單地說就是:

網頁視圖 - 第一URL,HTML表格 - 「返回」按鈕顯示,但不活躍

(當然)

Webview - 第二個URL,HTML詳細信息頁面 - '返回'按鈕顯示,並可以返回。

如何讓它只出現在第二個網址上,或者在第一個網址上被隱藏?

問候 蘭迪

EDIT(11月25日)

這裏是我的H:

#import <UIKit/UIKit.h> 
    #import "FilmnoirNavController.h" 


    @interface FilmnoirViewController : UIViewController <UIWebViewDelegate> { 
     IBOutlet UIWebView *webView; 
     IBOutlet UIBarButtonItem *backButton; 

    } 

    @property (nonatomic, retain) UIWebView *webView; 
    @property (nonatomic, retain) UIBarButtonItem *backButton; 

- (IBAction)backButtonClicked:(id)sender; 

    @end 

,這裏是我的L:

#import "FilmnoirViewController.h" 


@implementation FilmnoirViewController 

@synthesize webView; 
@synthesize backButton; 

- (IBAction)backButtonClicked:(id)sender { 
    [webView goBack]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Initialization code 
    } 
    return self; 
} 

/* 
Implement loadView if you want to create a view hierarchy programmatically 
- (void)loadView { 
} 
*/ 

/* 
If you need to do additional setup after loading the view, override viewDidLoad. */ 
- (void)viewDidLoad { 

    NSString *urlAddress = @"http://www.filmsite.org/filmnoir.html"; 

    //Create a URL object. 
    NSURL *url = [NSURL URLWithString:urlAddress]; 

    //URL Requst Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [webView loadRequest:requestObj]; 
} 

- (void)webViewDidFinishLoad { 
    // Other stuff if necessary... 

    // Could use hidden instead of enabled if you don't even want 
    // the button to be visible 
    backButton.enabled = (webView.canGoBack); 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 

- (void)dealloc { 
    [webView release]; 
    [super dealloc]; 
} 

@end 

我的IB佈局:

Filmnoir View Controller (Film noir) 
> View 
>>Web View 
>>Tool Bar 
>>>Bar Button Item (Back) 

以爲我像這樣的東西隱藏後退按鈕了吧:

- (void)webViewDidFinishLoad { 
    BOOL ableToGoBack = [webView canGoBack]; 

    if (ableToGoBack == NO) { 
     [backButton setHidden:YES]; 
    } 
    else { 
     [backButton setHidden:NO]; 
    } 

,但警告說,一個UIbarButton項目可能不setHidden迴應 - 而事實上並非如此。

回答

6

(webView.canGoBack == YES)時,您應啓用後退按鈕。您可以使用代理方法(如webViewDidFinishLoad:)來執行此操作。

- (void)webViewDidFinishLoad { 
    // Other stuff if necessary... 

    // Could use hidden instead of enabled if you don't even want 
    // the button to be visible 
    backButtonItem.enabled = (webView.canGoBack); 
} 

然後,對於backButtonItem你的「潤色Inside」的動作應該是這樣的。

- (IBAction)backButtonClicked:(id)sender { 
    [webView goBack]; 
} 
+0

感謝Sbrocket,但我仍然有越來越按鈕不在的WebView 1日URL功能相同的麻煩。當建議我試圖使用backButton.hidden我得到一個錯誤:請求成員'隱藏'不是結構或聯合。 感謝任何幫助,讓我的後退按鈕僅在webview'cangoback'時出現,並在不能出現時才顯示。 在編碼糟糕的邏輯我想是這樣的:後退按鈕=隱若webView的cangoBack = NO 歡呼球員蘭迪 我已經編輯inital問題,包括我的代碼。 – 2009-11-25 20:02:16

5

我知道這是有點舊,但我只是有這個問題。這是因爲我沒有將代表設置爲self。這是我的viewDidLoad:,以防其他人遇到這種情況。

- (void)viewDidLoad 
{ 
    [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:self.url]]; 
    [webView setDelegate:self]; 
    backButton.enabled = NO; 
    forwardButton.enabled = NO; 
    [super viewDidLoad]; 
} 

viewDidFinishLoad:

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    // snip ... 
    backButton.enabled = (self.webView.canGoBack); 
    forwardButton.enabled = (self.webView.canGoForward); 
} 
+0

謝謝,這真的幫助我! – dot 2012-02-11 19:51:11

+0

@mirthlab感謝其真正的幫助。但我有一個擔心。我工作的應用程序和網址來自web服務,我在Uiwebview中顯示。一些網址只有內容,並有一些已在forworded鏈接也在頁面..所以我怎麼能得到這個頁面已forworded鏈接?取決於我想要顯示並隱藏回來的按鈕,所以善意地建議我該怎麼辦? – Hitarth 2012-04-24 07:11:52