2

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!

Alex Muller
  • 1,565
  • 4
  • 23
  • 42
  • Please post the code you're using to create the webview in the first place. How are you initializing your site? – sudo rm -rf Apr 15 '11 at 21:18

2 Answers2

8

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);
}
Anne
  • 26,765
  • 9
  • 65
  • 71
0

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.

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161