问题描述
我有一个 UIButton,我试图从一个 UIButton 事件中调用 2 个不同的操作,因为该按钮需要做两件事,但我需要按一定的顺序来做.
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?
IBActions 从同一个组件中被触发的顺序是什么?有没有办法按一定的顺序调用它们?
In what order do IBActions get fired from the same component? Is there a way to call them in a certain order?
推荐答案
这个问题的答案很简单,只是有点奇怪.IBAction 按字母顺序调用.请参阅下面的示例.
The answer to this is quite simple, all be it a bit strange. IBActions are called in alphabetical order. See below for an example.
以下是 6 个 IBAction 方法,它们都打印出它们是哪种方法.
Below are 6 IBAction methods all printing out which method they are.
在 .h 文件中...
- (IBAction)a:(id)sender;
- (IBAction)b:(id)sender;
- (IBAction)c:(id)sender;
- (IBAction)d:(id)sender;
在 .m 文件中...
- (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
如果您按字母顺序查看它们,它们被解雇如下;a, b, b, d, e, f
生成显示的日志.即使您重新安排 IBAction 或以不同方式链接它们,也会生成相同的日志.
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.
这个问题的简单答案是在你的方法名称之前添加字母,例如 aOne, bTwo, cThree
然后它们将按顺序调用,而如果你留下它们 One, 二, 三
由于这个字母顺序,它们将被称为一,三,二
.
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.
这篇关于当从同一个组件调用时,两个 IBAction 触发的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!