使用webview可以设置一个简单的"后退"按钮(在导航栏或顶部工具栏中),该按钮不显示WebView的第一个URL上的后退按钮 - 并且仅显示在第二个URL上回到第一个?
除非我弄错了,在许多混合原生/网络应用程序(如新闻应用程序)中,您经常会在表格中看到新闻文章(HTML页面而不是"在xcode中编程"表格),这些文章超链接到文章详细信息页面(再次,HTML而不是本地编码) - 我无法想象的是,详细信息页面(webview中的第二个URL)如何显示"后退按钮",但表格(webview中的第一个URL)没有按钮在这些类型的应用程序中显示?
目前,我有一个所描述的webview,在屏幕顶部的工具栏中有一个"后退"栏按钮项目(出口为WebView的'cangoback')但是当没有页面要去的时候,按钮是从一开始就可见的回到 -
我简单的是:
Webview - 第一个URL,HTML表 - "后退"按钮显示,但不活动(当然)
Webview - 第二个URL,HTML详细信息页面 - "后退"按钮显示,可以返回.
如何才能将其显示在第二个URL上,或者在第一个URL上隐藏?
关心兰迪
编辑(11月25日)
这是我的h:
#import#import "FilmnoirNavController.h" @interface FilmnoirViewController : UIViewController { IBOutlet UIWebView *webView; IBOutlet UIBarButtonItem *backButton; } @property (nonatomic, retain) UIWebView *webView; @property (nonatomic, retain) UIBarButtonItem *backButton; - (IBAction)backButtonClicked:(id)sender; @end
这是我的m:
#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)
以为我用这样的东西来隐藏backButton:
- (void)webViewDidFinishLoad { BOOL ableToGoBack = [webView canGoBack]; if (ableToGoBack == NO) { [backButton setHidden:YES]; } else { [backButton setHidden:NO]; }
但警告说UIbarButton项可能不响应setHidden - 实际上它没有.
您应该在何时启用后退按钮(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的"Touch Up Inside"操作应该如下所示.
- (IBAction)backButtonClicked:(id)sender { [webView goBack]; }
我知道这有点老了,但我遇到了这个问题.这是因为我没有将代表设置为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); }