What you’ll learn in this chapter:
- How to navigate between views without relying on a NavigationView hierarchy
- More advanced data flow techniques including @ObservableObjects, @StateObjects and @EnvironmentObjects
- First look at using animations in SwiftUI
What we’ll create 🚀
In this chapter, we will learn how to navigate between views in SwiftUI without relying on view hierarchies such as navigation and modal views. A concept that may seem trivial, but by understanding it deeply, we can learn a lot about the data flow concepts used in SwiftUI.
For learning how to navigate independently between different views in SwiftUI, it’s appropriate to start with an example that’s not too complex. Supposed we have an app with two different main views. ContentViewA contains an Image with a grumpy dog and a Button reading “Next”. The other main view, called ContentViewB contains an Image with a happy dog and a Button reading “Back”.
I encourage you to practice your SwiftUI skills by building up those views by yourself!
However, you can also download the starter project from here.
We want to connect those views in a way that when we tap on the buttons we navigate back and forth. We could accomplish this using a NavigationView, but in this chapter, we don’t want to use such a NavigationView hierarchy. Instead, we want both views to be independent of each other. So let’s get started!