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);
}
}
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];
}
Here is answer
@IBAction func reload(sender: UIBarButtonItem) {
webview.reload()
}
@IBAction func back(sender: UIBarButtonItem) {
webview.goBack()
}
@IBAction func forward(sender: UIBarButtonItem) {
webview.goForward()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With