mbaaz
mbaaz

Reputation: 130

What ordes does the IBActions fire in when multiple IBActions supplied for same event

As per my header, I am building a view with some UIButtons in it. I was thinking about hooking up 2 actions to one UIButton event, since that button needs to do two things, but I need to do them in a certain order.

No matter how I try, I can't seem to change the order in which the Actions fire. Is there a way for me to decide this, or is it random?

When I right-click the UIButton in Interface Builder, I see both Actions added to the same event, but no matter in which order they appear does the firing order change.

Hoping some of you out there can help me.

Upvotes: 1

Views: 289

Answers (3)

Ravi Garg
Ravi Garg

Reputation: 572

You will not get answer to this question in any book or even googling.

I have tried that personally and it work in alphabetical order according to IBActions Names. Strange but its a fact!!

Tested Example Below :

helloworld.h

-(IBAction)a:(id)sender;
-(IBAction)b:(id)sender;
-(IBAction)c:(id)sender;
-(IBAction)d:(id)sender;
-(IBAction)e:(id)sender;
-(IBAction)f:(id)sender;

Action)g:(id)sender;

**HelloWorld.m file**

-(IBAction)a:(id)sender
{
    NSLog(@"I clicked a");
}
-(IBAction)f:(id)sender
{
    NSLog(@"I clicked f");
}
-(IBAction)b:(id)sender
{
    NSLog(@"I clicked b");
}
-(IBAction)c:(id)sender
{ 
    NSLog(@"I clicked c");
}
-(IBAction)d:(id)sender
{
    NSLog(@"I clicked d");
}
-(IBAction)e:(id)sender
{
    NSLog(@"I clicked e");
}

Now link these actions to your Button in [Touch Up Inside] event in any order. You will find the same output as shown below :

Output

2012-03-19 15:35:34.548  I clicked a
2012-03-19 15:35:34.554  I clicked b
2012-03-19 15:35:34.564  I clicked c
2012-03-19 15:35:34.566  I clicked d
2012-03-19 15:35:34.568  I clicked e
2012-03-19 15:35:34.572  I clicked f

Cheers!!

Happy Coding!!

Upvotes: 2

Matt Wilding
Matt Wilding

Reputation: 20163

UIButtons are subclasses of UIControl, which use the target-action pattern to communicate events. The programatic interface for adding targets is exposed via the addTarget:action:forControlEvents: method. Hooking up IBActions in interface builder is just a visual way of using this same interface; the bundle loader will call that method on the unarchived button when loading the .xib file that contains it.

The key point is that the target argument in that method is added to an NSSet (or mutable subclass) internally by the UIControl. NSSets are, by definition, unordered. This means that when the button needs to enumerate its set of targets to dispatch events, the order of enumeration is not well defined.

Upvotes: 2

Caleb
Caleb

Reputation: 125007

Since there's no clear way to indicate the order in which the actions should be sent, you should not rely on the order. Instead, create a separate action that calls the two actions that you care about in the desired order:

- (IBAction)doFooAndBar:(id)sender
{
    [self foo:sender];
    [self bar:sender];
}

Upvotes: 0

Related Questions