Reputation: 783
I have hooked up an activityIndicator in the storyboard and created a webView programmatically. However when I load the VC, the program crashes complaining that the activityIndicator is nil. Why is this? I confirmed that the outlet is connected properly.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.google.com/")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
activityIndicator.startAnimating()
print("loadingwebpage")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("didfinishloadingwebpage")
activityIndicator.stopAnimating()
}
}
Upvotes: 2
Views: 677
Reputation: 1703
You have this line :
view = webView
this mean you assign created webView to viewController's view. thats why your indicator destroy and cause crash.
add webView to viewcontroller's subview and crash will gone. change:
webView.frame = self.view.bounds
self.view.addSubview(webView)
Upvotes: 1