Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create UIwebview reload + goback/forward button programmatically

How do Create UIwebview reload + goback/forward button programmatically.

How do i call the following functions

func reload()
func goBack()
func goForward()

updated current code :

import UIKit

class webviewViewController: UIViewController {
    @IBOutlet var webview: UIWebView!

    @IBAction func reload(sender: UIBarButtonItem) {
        webview.reload()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL (string: "http://www.google.com");
        let requestObj = NSURLRequest(URL: url!);
        webview.loadRequest(requestObj);
    }
}
like image 495
system21 Avatar asked Feb 08 '23 00:02

system21


2 Answers

According to the Apple Documentation for WebView (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebView_Class/)

You can just connect the respective button outlets like this :

@IBAction func goBack(sender: AnyObject) {
    self.webView.goBack()
}

@IBAction func reload(sender: AnyObject) {
    self.webView.reload()
}

@IBAction func forward(sender: AnyObject) {
    self.webView.goForward()
}

Do also check that canGoBack and canGoForward are set to true (read-only).

If the buttons are also created programmatically, you can add selector such as Selector("goBack") and create a func goBack() {}

For ObjectiveC you can do it like this:

- (IBAction)browserGoBack
{
    [self.webView goBack];
}

- (IBAction)browserGoForward
{
    [self.webView goForward];
}

- (IBAction)browserRefresh
{
    [self.webView reload];
}
like image 197
Lawrence Tan Avatar answered Feb 10 '23 14:02

Lawrence Tan


Here is answer

@IBAction func reload(sender: UIBarButtonItem) {

             webview.reload()

        }

@IBAction func back(sender: UIBarButtonItem) {

             webview.goBack()

        }
@IBAction func forward(sender: UIBarButtonItem) {

             webview.goForward()

        }
like image 31
system21 Avatar answered Feb 10 '23 12:02

system21