Summary
I am currently faced with an issue that always worked fine in Objective-C, but I can't seem to get it to work in Swift (specifically v3). This is all built using Storyboards. I have two separate view controllers:
- ViewController: WKWebView & UIButtonItems
- SideMenuTableView: UITableView & UIButtons
Currently, the SideMenu pops over, modally, on top of the ViewController and lists a set of actions via UIButtons. I guess I'll just go off of the most simplistic example, just to get the idea down. What I am trying to accomplish in this example is reloading the WKWebView (ViewController) from a UIButton tap (SideMenuTableView). Below is a more clear picture of what I am trying to get at if things are still unclear:
Currently, I can call the SideMenu with a simple Storyboard segue (Kind: Present Modally)
. Furthermore, I can dismiss the SideMenu which was implemented into a simple close()
function. However, when attempting to call the refresh function, I receive the following error message:
fatal error: unexpectedly found nil while unwrapping an Optional value
Code
import UIKit
import WebKit
class ViewController: UIViewController, UINavigationControllerDelegate, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let webURL = URL(string: "https://google.ca")
let webRequest = URLRequest(url: webURL!)
webView.load(webRequest)
func refresh() {
webView.reload()
}
}
import Foundation
class SideMenuTableView: UITableViewController {
@IBAction fileprivate func close() {
self.dismiss(animated: true, completion: nil)
}
@IBAction func refresh(sender: AnyObject!) {
ViewController().refresh()
close()
}
}
EDIT: Updated segue code.
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if let vc = segue.destination as? SideMenuTableView {
vc.delegate = self
}
}
}