我正在尝试在故事板Swift 2.3 iOS应用程序中创建WebView。但是,每当我使用WebView导航到View时,应用程序崩溃。我发布了WebView的故事板和代码。这是否表明我做错了什么?
故事板场景:
<!--WebView View-->
<scene sceneID="oin-Tm-kjC">
<objects>
<viewController title="WebView View" id="iti-7S-ISp" customClass="webViewViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="3bg-3D-Wcg"/>
<viewControllerLayoutGuide type="bottom" id="Mnm-nM-yzb"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="gnf-vw-fwi">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<webView contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="rVb-rt-chb">
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<connections>
<outlet property="delegate" destination="iti-7S-ISp" id="E7m-JN-oGx"/>
</connections>
</webView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<connections>
<outlet property="webView" destination="rVb-rt-chb" id="ldC-1p-1XM"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Dj4-qu-aUe" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="248.80000000000001" y="-631.0344827586207"/>
</scene>
webViewViewController.swift:
import UIKit
class webViewViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
let url = "https://www.google.com"
override func viewDidLoad() {
super.viewDidLoad()
let nsUrl = NSURL(string: url)
let nsUrlRequest = NSURLRequest(URL: nsUrl!)
webView.loadRequest(nsUrlRequest)
}
}
我在控制台中看到这一行:“Interface Builder文件中的未知类webViewViewController”
后跟:“***因未捕获的异常而终止应用程序'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:此类不是键值webViewViewController的键值编码。”
此外,如果我删除了与webViewViewController.swift的耦合,应用程序运行正常。在这种情况下,它只显示一个空的UIWebView。
答案 0 :(得分:0)
import UIKit
class webViewViewController: UIViewController {
@IBOutlet weak var webView: UIWebView!
let url = "https://www.google.com"
override func viewDidLoad() {
super.viewDidLoad()
let nsUrl = NSURL(string: url)
let nsUrlRequest = NSURLRequest(url: nsUrl! as URL)
webView.loadRequest(nsUrlRequest as URLRequest)
}
}