Multi-layered child viewcontrollers where delegation is becoming a nightmare - how should they communicate with each other?

StackOverflow https://stackoverflow.com/questions/17955842

Question

This is a serious problem for me right now. Working on a code base with multi-layered children viewcontrollers about 4-5 layers deep - this is a tablet app with a very high degree of decoupling between viewcontrollers. The 5th layer child viewcontroller wants to send a message to the top level viewcontroller to show a modal. The 4th layer and 3rd layer have different messages as well so basically now I have like 4 protocols and its getting pretty complicated to send messages from such child viewcontrollers nested deep within to an ancestor viewcontroller. Is there an easier way to do this?

We thought about using NSNotification but not particularly fond of it because of its difficulty in debugging it and subscription not being a clear dependency within our control. We also thought about using a singleton but statics and singletons are evil (right?) and cause problems around state and concurrency.

Any help is appreciated. Also I realize I realize this is a slightly open ended design based question but it has a very specific use case (like the one described above).

Was it helpful?

Solution

Is an interesting question, but I guess we need few hints, such as a scheme.
The first thought is why you need to add nested child? couldn't you manage using a simple stack and a single Container VC? It seems to be not correct how you manage your VCs. If they need to be notified about changes in the model, it should be only the visible to catch them, in the viewWill/Did/appear of the others you can ask fresh data again. If you need to inform each VC about their states, I think you should modify your hierarchy.
I imagine a Container VC with a stack or a set of VCs and visible VC propery. This Container will manage the exchange of info. As rdelmar said, it seems a really high degrees of coupling. You should rethink in way that sees communication only between container and visible VC, the others can refresh their data while they appear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top