helloimbrando
helloimbrando

Reputation: 655

NavigationLink doesn't work when View is embedded in a modal sheet

Weird problem. I have a view called SecondView which has a NavigationLink like so:

struct SecondView: View {
    var body: some View {
        NavigationStack {
            List {
                Section {
                    EmptyView()
                }
                //PowerUp Section
                Section {
                    VStack {
                        NavigationLink (destination: Powerup()) {
                            Text("2")
                        }
                        .isDetailLink(false)
                    }
                }
            }
        }
    }
}

This view is embedded in a modal view. The NavigationLink works fine when I preview SecondView on Xcode but doesn't when I preview the modal sheet. Doesn't work on an actual device or simulator too. Looks like a hierarchy problem or something like this. Can someone tell me where I'm wrong? Thanks.

Upvotes: 1

Views: 863

Answers (2)

malhal
malhal

Reputation: 30582

Since the old NavigationLink(destination:) has some severe limitations, you are supposed to use NavigationLink(value:) and .navigationDestination(for:) when using the new NavigationStack, e.g.

NavigationStack {
    List(parks) { park in
        NavigationLink(park.name, value: park)
    }
    .navigationDestination(for: Park.self) { park in
        ParkDetails(park: park)
    }
}

Upvotes: 2

helloimbrando
helloimbrando

Reputation: 655

Auto-solved the problem. I was embedding SecondView in the modal in the wrong way. The view should be wrapped in a NavigationView and a NavigationStack first. Maybe it's helpful to someone else.

Upvotes: 1

Related Questions