Search code examples
swiftuiswiftui-navigationlinknavigationviewswiftui-navigationstack

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.


Solution

  • 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.