使用 UIControl (特别是 UIButton)时如何对目标进行排序?
在我的应用程序中,我有多个 UIButtons,我为其添加了目标。
事实证明,首先执行最近添加的目标,然后执行剩余的目标。
例如,采用以下代码:
[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(someOtherAction:) forControlEvents:UIControlEventTouchUpInside];
如果我要触摸 button
内部,则 someOtherAction:
将在 someAction
之前调用。
那不是我想要的。我想对目标进行排序,以便我可以先在该按钮内部进行修改,称为 someAction:
ability,然后再进行其他操作 someOtherAction:
我不知道该在哪里首先,我应该子类化并重写 UIButton
中的某些内容,还是深入挖掘 UIControl
中的内容?
感谢您的帮助!
-大卫
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.
For example, take this code:
[button addTarget:self action:@selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(someOtherAction:) forControlEvents:UIControlEventTouchUpInside];
If I would touch up inside button
, someOtherAction:
would be called before someAction
.
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:
I'm not sure where to begin, should I subclass and override something in UIButton
or rather dig inside UIControl
?
Thanks for the help!
-David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单(尽管很hacky)的方法是复制所有操作选择器,将它们从目标中删除,然后重新添加除您希望首先发生的操作选择器之外的所有操作选择器。
但是,您的要求可能是糟糕的设计决策的副作用,因为这不是必要的行为。
首先发生某事的最终目标是什么?
为什么不能将这两种操作合并到一个以正确顺序执行选择器的操作中?
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?
您可以提供一个中间对象或代理对象,该对象始终接收按钮的操作,然后将其包含的数组中的相应操作转发到最终目的地。
执行此操作的一种可能方法是重写代理对象的 -forwardInitation: 和 -methodSignatureForSelector: 方法。请参阅 http://developer.apple.com/library/mac /#samplecode/ForwardInitation/Introduction/Intro.html
You could provide an intermediary, or proxy, object that always receives the button's action, and then forwards the appropriate one from an array it contains to the ultimate destination.
One possible way of doing this would be to override the proxy object's -forwardInvocation: and -methodSignatureForSelector: methods. See http://developer.apple.com/library/mac/#samplecode/ForwardInvocation/Introduction/Intro.html