Navigation in SwiftUI uses NavigationStack to customize and improve, how to customize navigation? How do I separate the navigation logic from my views? How to go back to specific screen or root screen?

Since navigation has undergone a huge shift in iOS 16, the old way of navigating is facing deprecation.

Now is the time to get into navigation and learn its ins and outs. In a previous article, I explained in detail how to do basic navigation between the two screens.

Exploring the topic of navigation in SwiftUI is like approaching a hidden cave with potentially many hidden and uninteresting things, and you may have asked yourself these difficult questions:

  • How to do custom navigation? In my application, I have more than 30 screens or even more?
  • How do I separate the navigation logic from my views?
  • What if I need some data from the 20th screen that only exists in the first screen?
  • How can I go back to a specific screen or the root screen if I want?
  • What if I have more than 5 complex objects with many properties that I want to move on different screens?

Many iOS developers and the community are asking endless questions and many are struggling to find solutions, especially with the latest Navigation API introduced by Apple in Navigation.

Don't worry! good news!

If you want, today we'll learn how to navigate between 20+ screens. We will learn how to deal with these points:

Complex and custom navigation in real-world scenarios, including navigating between more than 3 screens at once.
Move any data structure you want from one screen to another.
Return to the previous screen.
Return to the home screen (aka the root screen).
How to leverage the power of @EnvironmentObject in SwiftUI to handle navigation.
Let's start with the basics of NavigationStack.

There are a few things to note about the new navigation in iOS 16:

Guess you like

Origin blog.csdn.net/iCloudEnd/article/details/131389636