alexyorke
alexyorke

Reputation: 4319

Execute IBAction on a custom event

How would I be able to run an IBAction based on another event? For example I could use a piece of code to run an IBAction.

What I'm trying to do is have two different buttons run one or more IBActions.

Upvotes: 0

Views: 893

Answers (2)

Arrix
Arrix

Reputation: 2731

An IBAction is just a hint for the Interface Builder. It's actually another way of saying void.

#define IBAction void

So there is nothing special about it. In Interface Builder, you can connect touch event for different buttons to the same IBAction. You can also call IBAction methods from another IBAction method. Use the sender argument to identify the source of the event.

For example,

-(IBAction)buttonTapped:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSLog(@"tapped: %@", btn.titleLabel.text);
    [self anotherIBAction:sender];
}

Upvotes: 0

John Calsbeek
John Calsbeek

Reputation: 36537

You can hook multiple buttons up to the same IBAction, so you could do it that way.

But IBActions are just methods. For example, if you have some action doSomething:, you can just call it on an object:

[obj doSomething:nil];

Upvotes: 2

Related Questions