Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What order do two IBActions fire when called from the same component?

I have a UIButton and I am trying to call 2 different actions from 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?

In what order do IBActions get fired from the same component? Is there a way to call them in a certain order?

like image 744
Dummy Code Avatar asked Nov 22 '25 15:11

Dummy Code


2 Answers

Why not create a single, new method that is called by the button and runs your existing methods exactly the way you want?

like image 109
Alex Avatar answered Nov 24 '25 08:11

Alex


The answer to this is quite simple, all be it a bit strange. IBActions are called in alphabetical order. See below for an example.

Below are 6 IBAction methods all printing out which method they are.

In the .h file...

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

And in the .m file...

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

When I tap on the button linked to all these I get the following log...

2013-06-05 18:06:52.637 TestIBAction[49798:c07] a
2013-06-05 18:06:52.637 TestIBAction[49798:c07] b
2013-06-05 18:06:52.637 TestIBAction[49798:c07] c
2013-06-05 18:06:52.637 TestIBAction[49798:c07] d

If you look at them by alphabetical order they are being fired as follows; a, b, b, d, e, f producing the log shown. Even if you re-arranged the IBActions or link them differently the same log is produced.

The simple answer to this problem is to add letters before your method names such as aOne, bTwo, cThree and then they will be called in that order, whereas if you left them One, Two, Three they would be called as one, three, two due to this alphabetical order.

Hope this helps people. It had me stumped for the longest time.

like image 36
Dummy Code Avatar answered Nov 24 '25 08:11

Dummy Code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!