从同一组件调用时,两个IBAction将触发什么顺序? [英] What order do two IBActions fire when called from the same component?

查看:96
本文介绍了从同一组件调用时,两个IBAction将触发什么顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

从同一组件中触发IBAction的顺序是什么?有没有办法按一定顺序调用它们?

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 ,然后将按该顺序调用它们,而如果您将它们保留为 1、2、3 由于这种字母顺序,它们被称为一,三,二

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将触发什么顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆