1

Is it possible to reference a control in an application from a static function?

What I have is a Viewstack containing VBoxes stored in separate controls. Ex:

<mx:ViewStack id="content" width="100%" height="100%" resizeToContent="true">
    <controls:Login/>
    <controls:Dash/>            
    <controls:Input/>   
    <controls:Review/>
    <controls:Search/>  
</mx:ViewStack>     

Once I get logged in on my login control, I would like to change the selected index of my ViewStack. From my outside controls, I cannot reference my ViewStack by name. I can reference a public static function from an outside control however I cannot refer to the ViewStack from within that function. Any help is greatly appreciated.

JH

jay
  • 434
  • 1
  • 5
  • 25
  • Why can't you reference your ViewStack by name? Where are you in relation to your ViewStack? – Sam DeHaan Feb 28 '12 at 23:05
  • Can you post the code of the static function that you tested with and that doesn't work so that we understand your question? – sch Feb 28 '12 at 23:13

3 Answers3

0

You could reach it starting from FlexGlobals.topLevelApplication (if it is visible from there). Although, the design of such a thing may be questionable.

Eduardo
  • 8,362
  • 6
  • 38
  • 72
  • Eduardo/rajesh.adhi, I would vote both of your responses up, but I don't have enough cred. :-P. Thanks anyway for your help. – jay Feb 29 '12 at 13:30
0

Is it possible to reference a control in an application from a static function?

Generally no. A static function (or property) exists on the class itself. Whereas MXML Children--such as in a view stack--exist on a specific instance of the class. A class level function will know nothing about any specific instances of the class and will not be able to access properties on a specific instance.

However, you can pass an instance of a class into a static function and access the properties that way. something like this:

public static function doStuff(myViewStack:ViewStack):void{
 trace(myViewStack.id)
 // do other stuff
}

And call it like this:

MyClass.doStuff(content)
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
0

Normally you can have a singleton class where you can save the instance of the main application and if you view stack is resides inside your main application then you can do some thing like this

public static function changeIndex(index:int):void
{
    FlexGlobals.topLevelApplication.content.selectedIndex = index;
    //urappinstance.content.selectedIndex = index;
}
Triode
  • 11,309
  • 2
  • 38
  • 48
  • FlexGlobals worked perfectly for me. I was able to place this code in my public class and reference it from all of my controls. Thanks a lot for your help! – jay Feb 29 '12 at 13:27