Bart Gloudemans
Bart Gloudemans

Reputation: 1221

Please some details on the MXML tag <mx:SetEventHandler />

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: Howto removeEventListener with <mx:SetEventHandler />?

thanks Bart

Upvotes: 1

Views: 440

Answers (1)

Simon
Simon

Reputation: 80789

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.

Upvotes: 2

Related Questions