Compiler error with selectedChild in Flex 4 with code that worked in Flex 3

A customer filed a bug recently about a backwards compatibility issue that they ran into when trying to compile their Flex 3 application in Flex 4. Normally, your application should compile just fine, but, you might run into some behavioral differences between the two releases.  However, in the case described in bug SDK-25021, your application won’t even compile! When using the ‘selectedChild’ property for any of the MX Navigators like TabNavigator, ViewStack or Accordion, you will get the following error in Flex 4:

Error: Implicit coercion of a value of type mx.core:INavigatorContent to an unrelated type flash.display:DisplayObject.

myViewStack.removeChild(myViewStack.selectedChild)

This code compiled fine in Flex 3, but, won’t compile in Flex 4. Unfortunately, we had to loosen the type of the selectedChild property to get the MX navigators working with Flex 4 Spark content. The type of selectedChild was ‘Container’, now it is ‘INavigatorContent’. In Flex 4, both the Container and NavigatorContent classes implement INavigatorContent. You will need to make some minor tweaks to your Flex 3 code to have it compiling in Flex 4.

To resolve the compile error, just cast your selectedChild property to a Container. For example, here is the Flex 3 code converted to Flex 4 code:

Flex 3: myViewStack.removeChild(myViewStack.selectedChild)

Flex 4: myViewStack.removeChild(Container(myViewStack.selectedChild))
I hope this makes the migration easier. Good luck!

4 responses

  1. storeViews.selectedChild.addChild(new SupportView); flex 3
    how can it work on flex 4

  2. Excelent! thank you

  3. It heve helped me! Thanks!

  4. hello

    I tyred Container(child);
    but container is not a defined function.

    How to deal with it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: