Linux world
Linux world

Reputation: 3768

How would I create back, forward, and refresh buttons for a UIWebView programmatically?

I currently have a webview created but I do not want to use interface builder to create the back, forward, and refresh buttons. How would I create these buttons programmatically? I know how to create regular buttons with code, but as for webView delegate buttons, I am lost and have not been able to find many resources on it.

Upvotes: 4

Views: 8740

Answers (4)

Grant Isom
Grant Isom

Reputation: 150

A simpler way to enable or disable buttons is:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
   [backButton setEnabled:webView.canGoBack];
   [fwdButton setEnabled:webView.canGoForward];
}

Upvotes: 0

TylerC
TylerC

Reputation: 115

To enable/disable Back or Forward button instead of using KVO, we can use the following "hack"

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    if ([webView canGoBack])
        [backbutton setEnabled:YES];
    else
        [backbutton setEnabled:NO];

    if ([webView canGoForward])
        [fwdbutton setEnabled:YES];
    else
        [fwdbutton setEnabled:NO];
}

Upvotes: 0

James Eichele
James Eichele

Reputation: 119214

From the UIWebView documentation:

If you allow the user to move back and forward through the webpage history, then you can use the goBack and goForward methods as actions for buttons. Use the canGoBack and canGoForward properties to disable the buttons when the user can’t move in a direction.

Setting up the buttons would then use addTarget:action:forControlEvents: (as pointed out by Sven):

[myBackButton addTarget:myWebView
                 action:@selector(goBack)
       forControlEvents:UIControlEventTouchDown];

If you want to get fancy and enable/disable the buttons based on the canGoBack and canGoForward properties, you will have to add some KVO notifications to your UIController.

Upvotes: 7

anon
anon

Reputation:

You need to set the target and action for the buttons using addTarget:action:forControlEvents: to your web view.

Upvotes: 1

Related Questions