使用 UIControl(特别是 UIButton)时如何对目标进行排序? [英] How to sort order of targets when using UIControl (specifically UIButton)?

查看:34
本文介绍了使用 UIControl(特别是 UIButton)时如何对目标进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有多个 UIButtons 为其添加目标.

In my app I have multiple UIButtons for which I add targets.

事实证明,最近添加的目标首先被执行,然后是剩余的目标.

It turns out that the most recently added target is preformed first and then the remaining targets.

以这个代码为例:

[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];

[button addTarget:self action:@selector(someOtherAction:) forControlEvents:UIControlEventTouchUpInside];

如果我在 button 内部进行修饰,someOtherAction: 将在 someAction 之前被调用.

If I would touch up inside button, someOtherAction: would be called before someAction.

那不是我想要的.我想对目标进行排序,以便我可以在该按钮内部进行修饰,首先调用 someAction:ability 然后其他操作 someOtherAction:

That's not what I want. I want to sort the targets so that I can have a touch up inside of that button first called someAction:ability and then other action someOtherAction:

我不知道从哪里开始,我应该在 UIButton 中子类化并覆盖某些内容,还是在 UIControl 中挖掘?

I'm not sure where to begin, should I subclass and override something in UIButton or rather dig inside UIControl?

感谢您的帮助!

-大卫

推荐答案

最简单的(虽然有点老套)方法是复制所有的动作选择器,将它们从目标中移除,然后重新添加所有的除了你想先发生的那个.

The simplest (albeit hacky) way to do this would be to make a copy of all the action selectors, remove them from the target then re-add all of them except the one you want to have happen first.

但是,您的要求很可能是糟糕设计决策的副作用,因为这不是必要的行为.

However, your requirements are likely a side-effect of a poor design decision since this isn't behavior that should be necessary.

让事情先发生的最终目标是什么?

What is your end goal of having something happen first?

为什么不能将两个操作合并为一个以正确顺序执行选择器的操作?

Why can't you combine both actions in one that executes the selectors in the correct order?

这篇关于使用 UIControl(特别是 UIButton)时如何对目标进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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