i have made the game tic tac toe in swift .. the game is completed i am stuck on the last bit that when the user presses the play again button i want to set all the buttons images back to nil so that the user can start playing the game again. i used the tag property to set it back to nil. i coded this :
for i = 0 ; i < 8 ; i++ {
var button = view.viewWithTag[i] as! UIButton
button.setImage(nil, forState:.normal)
}
but it gives error
could not cast value of type UIView to UIButton
You need to check if the tag is configured right
for var i = 0 ; i < 8 ; i++ {
let subview = view.viewWithTag(i)
if subview?.isKindOfClass(UIButton) == true{
let button = subview as! UIButton
button.setImage(nil, forState:UIControlState.Normal)
}else{
print("Tag \(i) is not configured right")
}
}
Also,like @Dharmbir Choudhary said
If you use tag to get button,do not start with 0,because default tag is 0 it is easy to mess up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With