Peter Lam
Peter Lam

Reputation: 37

How to get the action from a button?

In my program, there are 5 five button. Different actions will be performed based on the choice of the button. However, I just assign one IBAction to all button. Is there any method that can help me to detect which button the users choose inside a single IBAction? Thank you so much!

Upvotes: 1

Views: 395

Answers (3)

Pochi
Pochi

Reputation: 13459

Assuming your buttons have their

@property (strong, nonatomic) UIButton *menuButton;

You can change the function like this:

- (IBAction)actionButtons:(UIButton*)sender {
    if ([sender isEqual:menuButton]) {
        NSLog(@"Menu Pressed");
    }
}

This way you can use the tags for something else (I use them to denote in what stage I am of the interface for example)

PD: You might want your button to be weak, i got them linked as strong for a reason.

Upvotes: 1

Evan Mulawski
Evan Mulawski

Reputation: 55344

Assign each of the buttons a unique tag property. Then, check this in your IBAction method:

- (IBAction)theActionDone:(id)sender
{
    UIButton *button = (UIButton *)sender;

    switch (button.tag)
    {
        case 1:
        {
            //do stuff for button one
            break;
        }

        ...
    }
}

Upvotes: 3

user1040049
user1040049

Reputation:

The sender will be the calling objects.

- (IBAction)theActionDone:(id)sender;

If you have many buttons calling theActionDone: above, the sender is the button calling (either the cell or button control, either way you can work your way up from there).

Upvotes: 1

Related Questions