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

一笑奈何 提交于 2019-12-05 18:48:19

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

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.

The order is indeterminate and you should not rely on the order as it may change in a future OS update.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!