Alex Muller
Alex Muller

Reputation: 1565

iPhone - How to have a custom browser be able to detect if back/forward buttons need to be enabled/disabled

Hey guys! I have a tumblr app that I am developing, and I have a button that pushes a modal view that contains a webview and a toolbar on the bottom with refresh, back, forward and done buttons. The back forward and refresh buttons work, however I want to be able to tell if the webview can actually go back/forward and if not, disable the button..I have tried the code below, and the image nor the enable/disable changes.

- (IBAction)refreshPage {
    [signUpWebView reload];
}

- (IBAction)goBack {
    [signUpWebView goBack];
}

- (IBAction)goForward {
    [signUpWebView goForward];
}


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    // Enable or disable back
    if ([signUpWebView canGoBack]) {
        [backOnePage setEnabled:YES];
        backOnePage.image = [UIImage imageNamed:@"backButton"];
    } else {
        [backOnePage setEnabled:NO];
        backOnePage.image = [UIImage imageNamed:@"backButtonDisabled"];
    }

    // Enable or disable forward
    if ([signUpWebView canGoForward]) {
        [forwardOnePage setEnabled:YES];
        forwardOnePage.image = [UIImage imageNamed:@"forwardButton"];
    } else {
        [forwardOnePage setEnabled:NO];
        forwardOnePage.image = [UIImage imageNamed:@"forwardButtonDisabled"];
    }

}

Any recommendations would be greatly appreciated!

Upvotes: 2

Views: 2151

Answers (2)

Anne
Anne

Reputation: 27073

I recommend binding the forward and backward button directly to the UIWebview.

enter image description here

Automatically enable and disable like this:

- (void)webViewDidStartLoad:(UIWebView *)mwebView {
    backButton.enabled = (webView.canGoBack);
    forwardButton.enabled = (webView.canGoForward);
}

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

Upvotes: 8

sudo rm -rf
sudo rm -rf

Reputation: 29524

When you first initialize your webView, try starting it with a request, like this:

[signUpWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

So you're initializing the webView with loadRequest: instead of loadHTMLString:, or something similar.

Upvotes: 0

Related Questions