What is Event Handling?
Event handling means that when an event is fired then a specific action should be taken as changing the choices in one drop-down menu after the user makes a selection in another menu and revealing a block of information when the user moves the cursor over an image.
Types of Events
The events are predefined the framework. but events has two main categories: system events and user events. The system events are not caused by user actions but they are fired in response to special events happened in the system such as the application being fully loaded, components being created or responses being received like receiving response from network connection. the user events are the direct result of a user action like moving the cursor, typing in a text input or clicking a button, and more.
Ways of Handling Events
There are two ways of telling the application what actions to take when an event is fired. instructions can be placed inline or can be placed in a function and calling that function occurred when the event is fired.
Inline Event Handling
Handling events inline can be performed by assigning some ActionScript code to the component event property that wanted to be handled as seen in the following example.
<s:Label id="myLabel"/>
<s:TextInput id="myText" change="myLabel text=myText text"/>
In the previous example when the text written in the TextInput component changed the label text also changed and the action performed is written inline with the event change.
Function Event Handling
Functions provide a better way to handle events. to use functions to handle events we should define the function script block at first and then assign the name of that function as the value of the components event property in the MXML file as shown in the following example.
<s:Label id="myLabel"/>
<s:TextInput id="myText" change="changeLabelText()"/>
protected function changeLabelText():void
{
myLabel text=myText text
}
This approach does a better job of separating the presentation part from the logic part, allows handling events in some sophisticated manner, and will also allow code reuse as same function can be used to handle all sort of events on all components.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.