3

I have created a button over top of a UIWebView that I would like to use to refresh the webview when pressed. The following code is in my ViewController class:

@IBOutlet weak var webView: UIWebView!

override func shouldAutorotate() -> Bool {
    return false
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Landscape
}

override func viewDidLoad() {
    super.viewDidLoad()
    let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
    let button = UIButton(type: .System)

    webView.scalesPageToFit = true
    webView.multipleTouchEnabled = true
    webView.backgroundColor = UIColor.whiteColor()
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
    self.view.addSubview(webView)

    button.frame = CGRectMake(20, -20, 100, 100)
    button.setTitle("Refresh", forState: UIControlState.Normal)
    button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
    webView.insertSubview(button, aboveSubview: webView)
}

func reload() {
    webView.reload()
}

I have created the reload method to refresh the webView but nothing happens when I click the button. I also tried to create the reload method inside viewDidLoad to see if this fixed the issue. What could be causing this issue?

atwalsh
  • 3,622
  • 1
  • 19
  • 38

2 Answers2

3

You defined webview inside view did load scope, take it out:

let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))

func viewDidLoad() {
    super.viewDidLoad()
    let button = UIButton(type: .System)

    webView.scalesPageToFit = true
    webView.multipleTouchEnabled = true
    webView.backgroundColor = UIColor.whiteColor()
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
    webView.layer.zPosition = 1
    self.view.addSubview(webView)

    button.frame = CGRectMake(0, 0, 100, 100)
    button.setTitle("Reload Page", forState: UIControlState.Normal)
    button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
    webView.insertSubview(button, aboveSubview: webView)
}

func reload() {
    webView.reload()
}
Roy K
  • 3,319
  • 2
  • 27
  • 43
  • I updated the code in my question to show all code. I tried removing `webView` outside of viewDidLoad, and renamed it to remove conflicts with the @IBOutlet line, but the button still doesn't refresh the page. Any ideas? – atwalsh Aug 10 '16 at 02:34
  • Please put a breakpoint on line "webView.reload()" and see if it reached. Please update. – Roy K Aug 10 '16 at 05:50
  • The method is called when the app starts up and the breakpoint is reached then, but the breakpoint is not reached when the button is pressed. Could this be caused by the `action: Selector(reload())` part of the button addTarget? – atwalsh Aug 10 '16 at 20:17
3

action: Selector(reload())

The syntax is wrong. On Xcode version 7.2 and below it should be

action: Selector("reload")

on Xcode 7.3 and up it should be

action: #selector(ClassName.funcName)

In Xcode 7.2 whole thing should be

let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))

override func viewDidLoad() {
    super.viewDidLoad()

    let button = UIButton(type: .System)

    webView.scalesPageToFit = true
    webView.multipleTouchEnabled = true
    webView.backgroundColor = UIColor.whiteColor()
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
    self.view.addSubview(webView)

    button.frame = CGRectMake(20, -20, 100, 100)
    button.setTitle("Refresh", forState: UIControlState.Normal)
    button.addTarget(self, action: Selector("reload"), forControlEvents: .TouchUpInside)
    webView.insertSubview(button, aboveSubview: webView)
}

func reload() {
    webView.reload()
}
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
  • Thanks. Changing the action call of the button fixed my problem. I am now running into an issue when I remove the webview outlet I get the following error: `this class is not key value coding-compliant for the key webView`. Is this because a class name in the storyboard is different? – atwalsh Aug 20 '16 at 00:37
  • 1
    @atwalsh04 you can add `webView` from `storyboard` or by code programatically. My code example is adding by code. To fix the issue remove the `webview` view from `storyboard` – Warif Akhand Rishi Aug 20 '16 at 06:26