ios - swift: Adding an action to an array of buttons -
i'm designing simple sudoku app , need trigger action when 1 of 81 buttons clicked. created array of uibuttons in viewcontroller:
class sudokuboardcontroller : uiviewcontroller { @iboutlet var collectionofbuttons: array<uibutton>? override func viewdidload() { collectionofbuttons.addtarget(self, action: "buttonclicked:", forcontrolevents: uicontrolevents.touchupinside) ... } }
i'm able add buttons array storyboard ok, when try addtarget message:
value of type 'array<uibutton>?' has no member addtarget
is there solution problem not involve me creating 81 different outputs each button?
thanks help!
cheers
you need iterate through array of buttons , add target each of button. try out following code
var index = 0 button in collectionofbuttons! { button.tag = index // setting tag, identify button tapped in action method button.addtarget(self, action: "buttonclicked:", forcontrolevents: uicontrolevents.touchupinside) index++ }
Comments
Post a Comment