我在 Swift中编写了一个简单的WebView,但每当我尝试在iOS模拟器中启动它时,我都会遇到这些错误.出了什么问题? import UIKitclass ViewController: UIViewController {@IBOutlet var webview: UIWebViewvar urlpat
import UIKit class ViewController: UIViewController { @IBOutlet var webview: UIWebView var urlpath = "http://www.google.de" func loadAddressURL(){ let requesturl = NSURL(string: urlpath) let request = NSURLRequest(URL: requesturl) webview.loadRequest(request) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. loadAddressURL() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
错误:
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) self uiwebview.ViewController 0x7987fc70 0x7987fc70 request NSURLRequest * 0x78ebfc40 0x78ebfc40 requesturl NSURL * "" 0x78ec0040您只是没有将UIWebView连接到webview类属性
打开助手编辑器,在左侧显示你的xib或故事板,在右边显示你的视图控制器源文件,点击webview属性左边的圆圈并拖动到UIWebView控件.建立连接后,运行应用程序,它现在应该可以正常工作