5

使用 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 <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

这是我的米:

#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 - 事实上它没有。

4

2 回答 2

7

您应该在 时启用您的后退按钮(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];
}
于 2009-11-23T20:37:19.737 回答
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);
}
于 2011-04-05T21:06:04.377 回答