【发布时间】:2018-01-19 15:54:50
【问题描述】:
我有两个 swift 文件 MenuViewController.swift 和 ViewController.swift
我在 ViewController.swift 的以下函数中得到 nil 异常
@IBOutlet weak var webSiteView: UIWebView!
func LoadWebview(webURL: String) {
let url = NSURL(string: webURL)
let request = NSURLRequest(url: url! as URL)
webSiteView.loadRequest(request as URLRequest)
}
我使用 MenuViewController.swift 中的以下代码访问了该函数
ViewController().LoadWebview(webURL: "http://www.google.com")
我收到错误 Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value as the attached screenshot
错误截图 -> 1
【问题讨论】:
-
您的 ViewController 至少应该在调用 LoadWebview 函数之前调用 viewDidLoad()。此外,您还需要获取该控制器的实际加载实例,而不是一个新实例。
标签: ios swift null viewcontroller