Reputation: 6176
I would like to have three buttons or images in my view, that each represent a "weapon". How can I know which button has been selected by the user, and use this information?
I thought I would use one function to collect the information about the weapon selected, its damage, etc., but I'm used to creating one function for each button in the view. I'm wondering now how to determine the difference between those buttons, depending on which one has been selected.
Upvotes: 0
Views: 478
Reputation: 16316
You should create action methods that take one argument, the sender:
- (IBAction)weaponPressed:(id)sender;
You can then check the sender against instance variables pertaining to the buttons:
if (sender == gunWeaponButton)
// Do something
else if (sender == mineWeaponButton)
// Do something
else
// Do something else
Also, you can assign a tag to the buttons, which is simply an integer value:
gunWeaponButton.tag = 0;
Then you can check for the sender's tag:
if (sender.tag == gunWeaponButton.tag)
// Do something
Upvotes: 3