Yesterday it was the day that nothing I wrote worked 😭. I had to work on a project with a complex UI environment because of the complexity of the business behind. My task was simply to make it work as clean as possible.
The main screen was a UIViewController, lets call it ViewController A, with a container UIView as a child (purple area *). The container view was initialized in the storyboard with a Embed UINavigationController. The navigation controller had a UIViewController (root relationship), lets call it ViewController B. The controller’s B navigationController pushes a final UIViewController, lets call it ViewController C, by pressing a Submit button (yellow area *).
The business of the application stated that the top screen (red and green) should not be pushed or moved out of the view.
* see image below.
The Task: Pass data from the controller A to the embed controller B and then to the pushed controller C 😂.
The Problem: Controller A has no idea what is happening in the controller B, therefore it cannot pass its data to the next.
Realising that using an embed controller make it completely foreign to the controller A, made me think how I could fix this issue in the cleanest way possible. Instead of simply using the dirty Internet answer of “looping through the UIWindow controllers and finding the one you want” to access its data, which had to be public for this to work, I started narrowing down to problem, removed the embed controller and replaced it with two programmatically create-able views. One UIView for controller B and one UIView the same as controllers C view. I removed the controller C as it was no longer necessary.
Finally instead of having a embed UIViewController, I only controller A with two UIViews which I was adding and removing them from the superview. The custom UIViews had all the UI Elements needed and the controller A had a DataCoordinator object which now is handling all the necessary data.
Working on this made me think about solving UI issues without making the project “dirty”. I have learned that following the simple and fast solutions makes it work but as the project gets bigger, they become a burden. Next time I see a embed controller I will be more cautious 😎