0

I'm working on a SwiftUI app whose navigation hierarchy over multiple views looks something like this:

[List of Places] -> [Detail of one place] -> [Map]

On the map, not only the previously selected place is shown, but also all places that are known in the [List of Places]. The user should be able to select another place on the map and display its details. It would be possible to open another instance of [Detail of one place] from the map but this way the stack of views would become longer and longer. I think it is better to go back the complete navigation hierarchy and open another [Detail of one place] from [List of Places].

Navigate back: [Map] -> [Detail of one place] - > [List of Places] -> [Detail of one place]

However, I am not sure how to implement such an approach most skilfully. I could use

self.presentationMode.wrappedValue.dismiss()

and close each view one by one and go back in the navigation. From the [List of Places] I would then automatically switch to another detail view. So far I have not tried this yet but I think if I do it I will get an animation for each closed view. I don't want you to. Switching from the map to the details should work like normal navigation.

TalkingCode
  • 13,407
  • 27
  • 102
  • 147

1 Answers1

0

It seems to me you don't really have a view hierarchy if the user can go from [Detail of one place] to [Map] and from [Map] to [Detail of one place].

Given that, it might be best to create a single new view that shows either the [Detail of one place] or [Map] content, depending on the user action, and hides the other. If we call this new view [Combined], then your view hierarchy becomes:

[List of Places] -> [Combined]

The user would navigate from [List of Places] to [Combined]. By default, [Combined] would show the detail of one place. The user would tap some button there to see the map. [Combined] would hide the detail of one place, and show the map, but there would be no navigation on the stack.

If the user taps a different place on the map, [Combined] would hide the map and show the detail of that place. In this way, the user could switch between detail and map seamlessly.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • I tried it now and it works fine. But it somehow feels wrong when the view just switch so suddenly without navigation. – TalkingCode May 23 '20 at 17:50
  • This post shows how to pop to the root view controller in SwiftUI: https://stackoverflow.com/questions/57334455/swiftui-how-to-pop-to-root-view. It seems you might be able to use this approach to pop from [Map] to [List of Places], and then from there navigate to [Detail of one place]. – Mike Taverne May 24 '20 at 04:51