I've implemented a navigation stack that appends the next view's view model so that the appending and .navigationDestination looks like the below. Whenever I press the continue button to add the viewModel to the path in the ContentView (the child of BumperScreen and the second in the path), the app just stops. I tried isolating the problem with a test project using the same methodology for navigation and have been able to navigate several views, almost infinitely. I cannot replicate this issue and am having a hard time understanding why one is working, and not the other.
struct BumperScreen: View {
@State private var path = NavigationPath()
@StateObject var viewModel: BumperScreenViewModel
@StateObject var sheetManager = SheetManager()
init(viewModel: @autoclosure @escaping () -> BumperScreenViewModel) {
self._viewModel = .init(wrappedValue: viewModel())
}
var body: some View {
if isDoneOnboarding {
HomeView()
.environmentObject(sheetManager)
} else {
NavigationStack(path: $path) {
ZStack {
Color(.Gold)
.ignoresSafeArea()
VStack {
if show {
Spacer()
loadAnimation
.frame(width: 150, height: 150)
.task {
try? await viewModel.getDataFromAPI()
try? await Task.sleep(for: Duration.seconds(1))
path.append(ContentViewViewModel())
doneLoading.toggle()
show.toggle()
}
Spacer()
} else if doneLoading {
EmptyView()
} else {
launchAnimation
}
}
.navigationDestination(for: ContentViewViewModel.self) { model in
ContentView(viewModel: model, path: $path)
.environmentObject(sheetManager)
}
}
}
}
}
}
Which leads the user to the second view
struct ContentView: View {
@EnvironmentObject var sheetManager: SheetManager
@StateObject var viewModel: ContentViewViewModel
@Binding var path: NavigationPath
var body: some View {
ZStack {
Color(.trulliGold)
.ignoresSafeArea()
VStack {
Spacer()
headerStatement
Spacer()
VStack {
privacyPolicy
bluetoothPolicy
locationsPolicy
notificationsPolicy
apprunningPolicy
}
Spacer()
Spacer()
continueButton
}
}
.popup(with: sheetManager)
.navigationBarBackButtonHidden()
}
var continueButton: some View {
Button(action: {
print("pressed")
path.append(WelcomeScreenViewViewModel())
}) {
Text("CONTINUE")
}
.navigationDestination(for: WelcomeScreenViewViewModel.self) { model in
WelcomeScreenView(viewModel: model, path: $path)
}
}
}
Which goes to
struct WelcomeScreenView: View {
@StateObject var viewModel: WelcomeScreenViewViewModel
@Binding var path: NavigationPath
var body: some View {
ZStack {
Color(.trulliGold)
.ignoresSafeArea()
VStack(alignment: .center, spacing: 8) {
header
Spacer()
tabView
Spacer()
Spacer()
nextButton
closeButton
}
}
.navigationDestination(for: SetupGuideSpeakerSearchViewViewModel.self) { model in
SetupGuideSpeakerSearchView(viewModel: model, path: $path )
}
.navigationBarBackButtonHidden()
}
}
I've tried isolating the issue to a debug project and have been able to navigate several views therein with no issue. debug project