Sunday, March 28, 2010

Change States using Menu Bar

I am new to flex. The first website I made I used states for navigation. I made states for another small site I am working on.

This is the problem I have, I need to change states using the MenuBar. I know I should listen for clickevents but I am not sure how to do the

linking. Most online help and reference material I have use Navigation Containers. However I don't want to change the states.

Just wondering if someone has some code which uses menu bar and states together.

Change States using Menu Bar

Hi there

Take a look at this example;

%26lt;?xml version=''1.0''?%26gt;

%26lt;mx:Application xmlns:mx=''http://www.adobe.com/2006/mxml'' creationComplete=''initCollections();'' %26gt;

?%26lt;mx:states%26gt;

?%26lt;mx:State name=''state2''%26gt;

?%26lt;mx:RemoveChild target=''{label1}''/%26gt;

?%26lt;mx:AddChild position=''lastChild''%26gt;

?%26lt;mx:Label text=''THIS IS STATE 2''/%26gt;

?%26lt;/mx:AddChild%26gt;

?%26lt;/mx:State%26gt;

?%26lt;/mx:states%26gt;

?%26lt;mx:Script%26gt;

?%26lt;![CDATA[

?import mx.events.MenuEvent;

?import mx.controls.Alert;

?import mx.collections.*;

?[Bindable]

?public var menuBarCollection:XMLListCollection;

?private var menubarXML:XMLList =

?%26lt;%26gt;

?%26lt;menuitem label=''Menu1'' data=''top''%26gt;

?%26lt;menuitem label=''First State'' data=''''/%26gt;

?%26lt;menuitem label=''Second State'' data=''state2''/%26gt;

?%26lt;/menuitem%26gt;

?%26lt;/%26gt;;

? private function initCollections():void {

?menuBarCollection = new XMLListCollection(menubarXML);

?}

?private function menuHandler(event:MenuEvent):void?{

?currentState = event.item.@data;

?}

]]%26gt;

?%26lt;/mx:Script%26gt;

%26lt;mx:MenuBar labelField=''@label'' itemClick=''menuHandler(event);''

?dataProvider=''{menuBarCollection}'' /%26gt;

%26lt;mx:Label text=''THIS IS STATE 1 - DEFAULT'' id=''label1''/%26gt;

%26lt;/mx:Application%26gt;

I hope this helps, make sure you call your states the same as the data attribute in the XML

Andrew

Change States using Menu Bar

Thanks Andrew, I am going to try this out and see if it works.

  • eye make up
  • No comments:

    Post a Comment