https://github.com/nikolainobadi/swiftuinavdestinationbug
A SwiftUI project demonstrating a bug where nested navigation fails.
https://github.com/nikolainobadi/swiftuinavdestinationbug
bug swift swiftui swiftui-demo xcode
Last synced: about 1 year ago
JSON representation
A SwiftUI project demonstrating a bug where nested navigation fails.
- Host: GitHub
- URL: https://github.com/nikolainobadi/swiftuinavdestinationbug
- Owner: nikolainobadi
- License: mit
- Created: 2025-02-28T22:23:07.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-28T22:26:31.000Z (about 1 year ago)
- Last Synced: 2025-03-01T00:33:03.380Z (about 1 year ago)
- Topics: bug, swift, swiftui, swiftui-demo, xcode
- Language: Swift
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SwiftUINavDestinationBug
This project demonstrates a bug in SwiftUI related to nested navigation when using different `.navigationDestination` modifers in the same hierarchy.
## Overview
The app consists of two tabs:
- **"Ok" Tab** (Green)
- Uses `NavigationPath` to navigate to `ItemRouteChildStack`.
- Further navigation inside `ItemRouteChildStack` using `NavigationLink` works as expected.
- **"Bug" Tab** (Red)
- Uses `.navigationDestination(item:)` to navigate to `ItemRouteChildStack`.
- Further navigation inside `ItemRouteChildStack` using `NavigationLink` does not work.
## Reproducing the Issue
1. Run the app.
2. In the **"Ok" Tab**, tap the button.
- The app navigates to `ItemRouteChildStack`.
- Selecting any item in the list successfully navigates to another view.
3. In the **"Bug" Tab**, tap the button.
- The app navigates to `ItemRouteChildStack`.
- Selecting any item in the list does **not** navigate to a new view.
## Code Breakdown
### Working Navigation (Uses `NavigationPath`)
```swift
struct ContentView: View {
@State private var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
Button("this path works") {
path.append(MyItem(name: "something"))
}
.navigationDestination(for: MyItem.self) { myItem in
ItemRouteChildStack(item: myItem)
}
}
}
}
```
- `NavigationPath` correctly manages navigation state.
- Further navigation inside `ItemRouteChildStack` works.
### Broken Navigation (Uses .navigationDestination(item:))
```swift
struct BugContentView: View {
@State private var myItem: MyItem?
var body: some View {
NavigationStack {
Button("This path is broken") {
myItem = MyItem(name: "something")
}
.navigationDestination(item: $myItem) { myItem in
ItemRouteChildStack(item: myItem)
}
}
}
}
```
- The initial navigation works.
- However, further navigation inside `ItemRouteChildStack` fails.
## Expected Behavior
Both approaches should allow further navigation inside `ItemRouteChildStack`.