English 中文(简体)
Navigate a WebView
原标题:

With a webview is it possible to set up a simple Back button (either in a navigation bar or a top toolbar) that doesn t show the back button on the first URL of the WebView - and only appearing on a second URL to get back to the first?

Unless I m getting getting this wrong, in a lot of hybrid native/web apps such as News apps, you often see news articles in a table (HTML page rather than a programmed in xcode table) which hyperlink to article detail pages (again, HTML rather than natively coded) - what I can t figure, is how the detail page (2nd URL in webview) displays with a back button but the table (1st URL in webview) doesn t have the button showing in these type of apps?

Currently, I have a webview as described, with a back bar button item in a toolbar at the top of screen (outlet as cangoback for WebView) but the button is visible right from the start when there s no page to go back to -

What I ve got simply is:

Webview - 1st URL, HTML table - back button shows, but isn t active (of course)

Webview - 2nd URL, HTML detail page - back button shows, and can go back.

How do you get it to only appear on 2nd URL, or be HIDDEN on 1st URL?

Regards Randy

EDIT (25th November)

Here s my 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

and here s my 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  

My IB layout:

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

Thought I had it with something like this to hide the backButton:

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

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

but warning says a UIbarButton item may not respond to setHidden - and indeed it doesn t.

问题回答

You should enable your back button when (webView.canGoBack == YES). You can do this in a delegate method like 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);
}

Then, your "Touch Up Inside" action for the backButtonItem should look like this.

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

I know this is a little old, but I just had this problem. It was because I hadn t set the delegate to self. Here s my viewDidLoad: just in case someone else runs into this.

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

and viewDidFinishLoad:

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




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签