Search code examples
iosswiftuiwebview

Reloading a WebView with a new url from another ViewController


I'm having trouble reloading a UIWebiView from another view controller. When I try to send the new url and reload it, the app crashes due to the webview being nil.

I made a simple test app to narrow down my problem, right now its a simple page with a webivew showing google and a button on the view. When the button is pressed, it modally presents another page. This page only has one button that when pressed, it dismissed itself and calls a function in the first view asking to refresh it with a new url. Heres my code:

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let urlRequest = NSURLRequest(URL: NSURL(string: "https://www.google.com")!)
        webView.loadRequest(urlRequest)
    }

    @IBAction func showViewPressed(sender: AnyObject) {
        let showView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("showView")
        self.presentViewController(showView, animated: true, completion: nil)
    }

    func reloadWebView(newURL: String) {
        let urlRequest = NSURLRequest(URL: NSURL(string: newURL)!)
        webView.loadRequest(urlRequest)
    }

}

and for the second view:

class ReloadViewController: UIViewController {

    @IBAction func buttonPressed(sender: AnyObject) {
        dismissViewControllerAnimated(true, completion: {
            ViewController().reloadWebView("https://www.yahoo.com")
        })
    }

}

it crashes with fatal error: unexpectedly found nil while unwrapping an Optional value


Solution

  • Alright, so it seems before I ask a question I spend hours trying to find a solution to no avail. Then 5 minutes after I post a question thinking i'll never find the solution... I find it. So I found a solution that was for a tableview here this also happened to apply to webviews. So for anyone in the future looking for the same thing, heres what I did:

    class ViewController: UIViewController {
    
        @IBOutlet var webView: UIWebView!
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let urlRequest = NSURLRequest(URL: NSURL(string: "https://www.google.com")!)
            webView.loadRequest(urlRequest)
    
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reloadWebView), name:"reload", object: nil)
    
        }
    
        @IBAction func showViewPressed(sender: AnyObject) {
            let showView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("showView")
            self.presentViewController(showView, animated: true, completion: nil)
        }
    
        func reloadWebView() {
            let urlRequest = NSURLRequest(URL: NSURL(string: "https://yahoo.com")!)
            webView.loadRequest(urlRequest)
        }
    
    }
    

    and for the second view:

    class ReloadViewController: UIViewController {
    
        @IBAction func buttonPressed(sender: AnyObject) {
            dismissViewControllerAnimated(true, completion: {
                NSNotificationCenter.defaultCenter().postNotificationName("reload", object: nil)
            })
        }
    
    }
    

    and it worked beautifully!