I’m making a two-dimensional array of buttons for a game I’m making. The actual array holds instances of a square class, which has (among other things) the X and Y coords of where the button should go. The main “X coordinate” array is a class object, but the “Y coordinate” arrays are created programmatically, as are all the instances of the square class. So far, I’ve got the buttons showing in the proper locations, but I need a way for the buttons to know where they are in terms of X and Y coordinates. I want to make it so that when a button is clicked, its image changes to a “selected” state, but I don’t know how to access the specific button that is clicked, since it’s unconnected to the square object that holds the coords. I’ve been toying with using addTarget:action:forControlEvents:, but I don’t know how to pass the coords to a selector.
I don’t think I’ll need to post any code for this question, as I don’t have an actual coding error, but if you’d like to see some of my code, or if you need more information, just ask, and I’d be happy to provide either.
Have you tried using tags ? Set them with
button0.tag = 0, button1.tag = 1, .... When you add the action, pass the tag of the button, and in your action, update the button with the correct tag.Note: I may be wrong on this, I’m not sure I understood exactly your context, but that’s how I would identify different elements of the same type, so it may be usable here :). If I’m totally wrong, I’m sorry.