user5535374
user5535374

Reputation:

Trying to change the colour of a UIToolbar in Swift after Switch is tapped

I am trying to change the colour of my UIToolbar when a switch is tapped but it always comes up with this error:

"fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)"

This is the function that is activated once the switch is tapped:

 func themeDark() {
        ViewController().toolbar.barStyle = UIBarStyle.Black
    }

Upvotes: 0

Views: 42

Answers (1)

bytor99999
bytor99999

Reputation: 991

Doesn't XCode show you that you have to add "!" to the variable. Also wouldn't it have been easier if you had an IBOutlet for the toolbar already in your ViewController. Then you can access it via the variable

so having the outlet like

@IBOutlet weak var toolbar: UIToolbar!

then

toolbar.barStyle.

Also, if you show us some of your code, it would help a lot. I am making too many assumptions here too.

Wait, Where do you see a UIBarStyle class. I don't have such a class in my XCode. I see a UIBarButtonItemStyle.

Upvotes: 0

Related Questions