Messages between Containers

Hi all,

I am trying to find a better way to handle some code I really don’t like right now.

Ok - So, I have a basic hierarchy of a WebPage object and a container within it that is the main container. This container contains 3 items, 2 images and another container with the menu controls.

The menu controls need to instruct the main container to load in a new container and hide the menu (so the images are still visible through the new container)

Now, I have this working, but it is very convoluted with having to hand off references between the containers which is making it more and more confusing to track through the code.

Coming from iOS, I’d either setup delegation or setup a given object to listen for certain messages. I’m not sure how to replicate that here. I was first thinking of custom events, but I don’t see a nice way to handle that between two different objects that are in a has-a relationship (where the sub-object needs to send a message to its containing object).

So if you can imagine:

page---->MainContainer
|
|—> DataDisplay (dynamically created and embedded into the container)

DataDisplay needs to let the MainContainer know that the back button has been pushed so it can be unloaded and the actual main menu made visible and enabled. Right now this is handled through reference variables. If I can fire a custom event or send a message for the MainContainer to receive to act on, that would be ideal!

Off the top of my head

Maybe create a new generic class that defines new events as needed
This will act as the common abstract super class for all you “data display classes”
Make a constructor method on it that is private so you can’t actually create new instances
Then your DataDisplay inherits from that super - it cannot inherit UI though

You can add events to this abstract base class

Now you can raise whatever events the abstract base class defined at appropriate points in your subclass code

When you embed one you can add a handler and respond to the events
Just be careful to remove the handler when you close the instance