Search code examples
swiftuiprogressview

Unexpectedly found nil in swift with UIProgressView


I've created a progressView and linked it like this: progressview

And here is what I do in my view controller:

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

     @IBOutlet weak var progressView: UIProgressView!
     @IBOutlet weak var KronosWebsite: WKWebView!

     override func loadView() {
         //initialize the webview
         progressView = UIProgressView()
         
         progressView.progress = 1.0

         KronosWebsite = WKWebView()
         self.view = KronosWebsite
         self.view.addSubview(progressView)
     }
 }

And I've got

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional

in this line progressView.progress = 1.0


Solution

  • Add super.loadView() when you use loadView for a vc that originally inside storyboard

     override func loadView() {
         super.loadView() 
    

    or insert all code inside viewDidLoad , also don't re-init

    progressView = UIProgressView() 
    KronosWebsite = WKWebView()
    

    as they already initiated from storyboard