Search code examples
iosswiftcocoa-touchexc-bad-accessforced-unwrapping

EXC_BAD_ACCESS when setting properties of an option view property


I'm trying to figure out how to make a UIProgessView (loading Bar) have the color tint of green. I've looked around and there is no working Swift version for this. I am also trying to figure out how to take the bar off of the screen when it's finished. Nothing is done with the storyboard, everything is done programmatically.

I'm trying to customize the bar with this but it says "Bad access".

  self.progressView!.tintColor = UIColor.greenColor()

This is where I'm trying to hide the bar, but there is a bad access here as well.

  progressView!.hidden = true

The context:

import UIKit
import AVFoundation

class MainController: UIViewController {

    var progressView: UIProgressView?

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

    func addControls() {

        //----This where it try to change the tint below
        self.progressView!.tintColor = UIColor.greenColor()

        // Create Progress View Control
        progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
        progressView?.center = self.view.center
        view.addSubview(progressView!)
    }
}

Solution

  • You haven't actually created the UIProgressView before you call self.progressView!.tintColor thus, progressView is nil at this point but you're trying to force unwrap it.

    Move the progressView = UIProgressView(...) line up to the top of addControls() and that should solve the problem.