Search code examples
swiftsegueuigesturerecognizergestureaddsubview

Swift fatal error: unexpectedly found nil while unwrapping an Optional value on segue


I know this is a crazy asked question, but I still can't figure it out. I am using a library for creating a circular progress bar (KDCircularProgress). Everything works fine, but the moment I try to call and perform a segue (for changing the ViewController), the app crashes with the "optional value" error.

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    ...
    // Gauge pressure
    var frontGaugeProgressLeft:  KDCircularProgress! //define the variable here
    ...

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        frontGaugeProgressLeft = KDCircularProgress(frame: CGRect(x: 0, y: 0, width: 125, height: 125))
        frontGaugeProgressLeft. ... //do some settings here

        // And display the progress bar to the view
        view.addSubview(frontGaugeProgressLeft) //if this is commented, app works

        ...
    }

    ...

    // Show settings/preferences menu
    func panAction(sender: UIScreenEdgePanGestureRecognizer) {
        if sender.edges == UIRectEdge.Right {
            performSegueWithIdentifier("showMenuSegue", sender: nil)
        }
    }

    ...

}

When I swipe left and trigger the segue, for changing the ViewController, the app crashes. I narrowed it down to that view.addSubview line. If I comment the line, app works. Tried some things, but still no clue how to fix this. Appreciate the support!


Solution

  • Fixed. There was some method in the library. Commented that since it was not used, everything alright.

    //    public override func didMoveToWindow() {
    //        progressLayer.contentsScale = window!.screen.scale
    //    }