Skip to main content
GameDev.net gamedev.net
🔒 Locked

iPhone Dev - Which button was pressed?

Started by zyrolasting Jun 24, 2010 at 7:37 AM 1 replies 1.5k views
Original Post
zyrolasting
zyrolasting
I have a NSMutableArray of cards for a memory game clone, where you flip up matching cards to score points.

The card class just has a UIButton and unsigned int used to check for matching cards. I need to know what button is pressed when the user taps one so I know what cards to compare. I do not know how to retrieve this information, and I've not having much luck finding out.
dustin10
dustin10
You can use the "tag" property of the UIButton class to identify the buttons. You can set it in Interface Builder. If you hook up the "touch up inside" event of all the buttons to the same IBAction then you can do something like this:

- (IBAction)buttonClicked:(id)sender {    UIButton *button = (UIButton*)sender;        // maybe have an NSArray of Card objects that maps the tag to the index in the array    Card *card = [cardsMap objectAtIndex:button.tag];    // now card is the one that was pressed}
zyrolasting
zyrolasting
Thanks! I actually spoke to someone today about another solution since I do not have a static number of buttons. I did not know that using addTarget:action:forControlEvents: set things up to pass the sender. I should be able to solve my problem if my card is a UIButton.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.