How to sort order of targets when using UIControl (specifically UIButton)?

心已入冬 提交于 2019-12-11 01:09:03

问题


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


回答1:


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?




回答2:


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



来源:https://stackoverflow.com/questions/8828740/how-to-sort-order-of-targets-when-using-uicontrol-specifically-uibutton

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