How will I program the back, forward and refresh softkeys for UIWebView?

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

+4
source share
4 answers

From the UIWebView documentation:

If you allow the user to move back and forth through the history of the web page, you can use goBack and goForward as actions for buttons. Use the canGoBack and canGoForward properties to disable buttons when the user cannot move in the direction.

Button customization will then use addTarget:action:forControlEvents: (as indicated by Sven ):

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

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

+7
source

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

+1
source

To enable / disable the "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]; } 
0
source

A simpler way to enable or disable buttons:

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

All Articles