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