As per my header, I am building a view with some UIButtons in it.
I was thinking about hooking up 2 actions to 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?
When I right-click the UIButton in Interface Builder, I see both Actions added to the same event, but no matter in which order they appear does the firing order change.
Hoping some of you out there can help me.
UIButtons are subclasses of UIControl, which use the target-action pattern to communicate events. The programatic interface for adding targets is exposed via the
addTarget:action:forControlEvents:method. Hooking up IBActions in interface builder is just a visual way of using this same interface; the bundle loader will call that method on the unarchived button when loading the .xib file that contains it.The key point is that the
targetargument in that method is added to an NSSet (or mutable subclass) internally by the UIControl. NSSets are, by definition, unordered. This means that when the button needs to enumerate its set of targets to dispatch events, the order of enumeration is not well defined.