OK, we have now accomplished some incredible things, and to be honest, when we first sat down to see how Swift’s new observation tools could be applied to UIKit we never dreamed we would be able to get this far. We can now drive navigation in UIKit controller directly from state in a concise manner, and it looks very, very similar to how one does navigation in SwiftUI.
This really does go to show that when domain modeling is a top priority in your application, you can build apps for seemingly very different paradigms with very similar code. I think most of us would agree that UIKit and SwiftUI are on opposite sides of the spectrum when it comes to framework design, yet we can see here that it is possible to build views in each style with a lot of similarities.
But we can push things even further with observation. We have fully covered the concept of tree-based navigation when it comes to observation, but there is another form of navigation to think about. Where tree-based navigation allows you to drive navigation from optionals and enums, there is something called “stack-based navigation”, where you drive navigation from a flat collection of states. When a value is added to the collection there is a drill-down animation to see a new view come onto the screen, and when a value is removed from the collection there is a pop animation to see the view leave the screen.
Let’s see how we can use the observation tools in Swift to give us a really nice way to implement stack-based navigation in our apps.