Group Group Group Group Group Group Group Group Group

Multi-Cast Delegate Pattern – Question

Hi everyone. Awesome book, learning lots! I’m having a go at making an app using the multi-cast delegate pattern. I have a couple of questions though.

Question 1
In the playground example, the delegate object variables are created like this:

In my app, my initial controller is View Controller A. The delegate objects are other View Controllers (B, C, & D). Do I just create the variables in my initial View Controller like this:

ViewControllerA

Question 2
If I type a location in the textField (shown below) & click send, View Controller B, C, & D print out a message to the console to show that the system is working. But, when I navigate to each by clicking the relevant tab bar icon (B, C, or D), the ‘location’ label on each view controller’s screen hasn’t held onto the variable at all.

I would have thought that the value would have persisted, at least until the screen was visited, but it seems as though the value is not held in memory at all? Is this normal?

I wonder if I have to actually manually persist the location value (String) for each View Controller so that it can be recalled when navigating to each particular screen? Thoughts??

Thanks!! :relaxed:

@jrg.developer Can you please help with this when you get a chance? Thank you - much appreciated! :]

If this is just a “test” app, sure, do whatever you’d like –– it’s throwaway code that won’t ever ship, after all.

If this was actually meant for production, then no, I would not do this.

Why? You’ve tightly coupled the ViewController to ViewControllerB, ViewControllerC and ViewControllerD here. Its responsibility is likely to grow exponentially –– it will quickly become a “god” object because it has intimate details of these types.

Instead, I’d ask myself:

What am I trying to actually do?

For example, is this meant to be a container view controller of some sort?

If so, can I depend on an abstract, base UIViewController instead? Or even better, a protocol?

First, make sure you understand how a container view controller works.

Essentially, you haven’t actually rendered the view by instantiate the view controllers, which is all you do in the code you’ve shown.

Instead, you actually need to set the value on a view. For example, you might do this in viewDidLoad() initially.