ios - Distinguish a UIButton by tag -
i've got game board (an 8x8 array). want implement drawing function, each cell in board uibutton separate tag (from 0 63). how through buttons (button separate tags) being able use setimage() function?
a piece of code:
for in 0..<8 { j in 0..<8{ if gameboard[i,j].rawvalue == 1 { cellbutton.setimage(uiimage(named:"whitepiece.png"), for: uicontrolstate()) } else if (gameboard[i,j].rawvalue == 2){ cellbutton.setimage(uiimage(named:"blackpiece.png"), for: uicontrolstate()) } } }
the code posted doesn't make sense. looks have instance variable cellbutton, , you're trying use somehow set image on 64 of buttons. can't possibly work. if have tags on buttons use this:
say define tag numbers being in range 100 188, 10's digit row number , 1s digit column number:
for in 0..<8 { j in 0..<8{ //create tag number cell let tagnumber = 100 + * 10 + j guard let acellbutton = view.viewwithtag(tagnumber) as? uibutton else { print("error. can't find button tag \(tagnumber)") continue } if gameboard[i,j].rawvalue == 1 { acellbutton.setimage(uiimage(named:"whitepiece.png"), for: .normal) } else if (gameboard[i,j].rawvalue == 2){ acellbutton.setimage(uiimage(named:"blackpiece.png"), for: .normal) } } }
Comments
Post a Comment