Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 780
randy fernando Avatar asked Nov 23 '09 20:11

randy fernando


2 Answers

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];
}
like image 75
Bryan Henry Avatar answered Oct 05 '22 03:10

Bryan Henry


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);
}
like image 37
markquezada Avatar answered Oct 05 '22 05:10

markquezada