Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not cast value of type

I am creating a Tic Tac Toe app. When the game resets, all the Xs and Os UIButton images should return to nil. Each space in the board is tagged from 0 to 8.

  var buttonsToClear : UIButton

    for var i = 0; i < 9; i++ {

        buttonsToClear = view.viewWithTag(i) as! UIButton

        buttonsToClear.setImage(nil, forState: .Normal)

    }

Everything worked fine until I added a Navigation Bar. Now I get the error message

"Could not cast value of type '_UINavigationBarBackground' (0x107e2f2a0) >to 'UIButton' (0x107e357e0). (lldb)

I am absolutely certain that my new Navigation Bar has a tag of "10".

Navigation Bar Tag

What else could be triggering this message?

like image 524
Ryan Lague Avatar asked Jan 07 '23 18:01

Ryan Lague


1 Answers

You should not use 0 tag, due to 0 is default and you can get any view (in this case the background of the navigation).

Tag the views starting by 1.

like image 73
Jose Grosso Avatar answered Jan 15 '23 19:01

Jose Grosso