johnpatrickmorgan / NavigationBackport

Backported SwiftUI navigation APIs introduced in WWDC22

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'navigator.push' doesn't work in iOS 15 preview

sugarwyc opened this issue · comments

struct ContentView: View {
    @State var path = NBNavigationPath()

    var body: some View {
        NBNavigationStack {
            HomeView()
        }
    }
}

struct HomeView: View {
    @EnvironmentObject var navigator: PathNavigator

    var body: some View {
        VStack {
            Button {
                navigator.push(1)
            } label: {
                Text("Number 1")
            }
        }
        .padding()
        .nbNavigationDestination(for: Int.self) { number in
            Text("number: \(number)")
        }
    }
}

struct PreferencesView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

In iOS 15 preview
Tap the button, then go back, and then click the button again and there is no response.

#Preview {
    ContentView()
}

Generating a preview with the new API #Priview works fine.

I think you forgot to pass the EnvironmentObject like ContentView().environmentObject(...) in the preview.