views:

186

answers:

1

Can anyone give me some details on the proper use of the tag <mx:SetEventHandler /> used when switching states? The Flex documentation is not very elaborate on this subject. Especially removing event handlers has my interest.

This question is a more specific formulation of my preceding post: http://stackoverflow.com/questions/1973946/howto-removeeventlistener-with-mxseteventhandler

thanks Bart

+2  A: 

SetEventHandler pretty much does what it says on the tin. For a given state you can define an event handler for a chosen event. You provide arguments to it which define the event, the function which is to be caled in response to the event, and the target object which dispatches the event. The adobe docs are pretty clear about that much.

If I understand your question correctly you want to remove an event handler rather than set one for a different state. If that is the case then you have a couple of options, you can define a different handler for each state, some of which do nothing, or you can define nulls in the function definition in the SetEventHandler tax for a given state.

<mx:State name="hasHandler">
    <mx:SetEventHandler name="click" handlerFunction="doClick" target="{myClickableObject}"/>
<mx:State/>
<mx:State name="hasNoHandler">
    <mx:SetEventHandler name="click" target="{myClickableObject}"/>
<mx:State/>

leaving the handlerFunction undefined sets it to null.

However, what I would do would be to define SetEventHandlers for only those states that need them. You should be able to set up the logic of your states such that you never need to remove an event handler, only set them. I think this is closer to the intended use of SetEventHandler and saves you having to rely on setting nulls for some states.

HTH. If you google "Flex SetEventHandler" there are a lot of good resources about it.

P.S. Make sure your default state does not have the event specifically handled if you are going to use this method.

Simon
Thanks, that explains exactly what I needed. I understand that I should beware of improper use of removing event handlers. My question was mainly about the concept of the tag.An addition to you lines of code: the second to last line should probably be:`<mx:SetEventHandler name="click" target="{myClickableObject}" />`In order to remove the `click` eventhandling on specifically the `myClickableObject`
Brelsnok
Thanks for the code correction, you are right of course. I've updated the code above to reflect that.
Simon