0

I am trying to implement Apple's new NavigationStack and am having some trouble. I am able to navigate to the next view after a Bool gets set to true. Once I am in the next view however, it automatically goes back to the root view of the stack.

The only thing fix I've found is wrapping the new view that I am progressing to in a NavigationView as opposed to a NavigationStack and implementing a path.

The first View

struct BumperScreen: View {
    @State var show = false
    @State var isDone: Bool = false
    @State var doneLoading = false
    @State private var path: [Bool] = []
    @StateObject var viewModel: BumperScreenViewModel
    @StateObject var sheetManager = SheetManager()
    
    init(viewModel: @autoclosure @escaping () -> BumperScreenViewModel) {
        self._viewModel = .init(wrappedValue: viewModel())
    }
    
    var body: some View {
        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))
                                    doneLoading.toggle()
                                    path.removeAll()
                                    path.append(doneLoading)
                                    show.toggle()
                                    print("Done")
                                }
                            Spacer()
                            
                        } else if doneLoading {
                            NavigationLink(value: doneLoading) { EmptyView() }
                        } else {
                            launchAnimation
                        }
                    }
                }
                .navigationDestination(for: Bool.self) { doneLoading in
                    if doneLoading == self.doneLoading {
                        ContentView()
                            .environmentObject(sheetManager)
                    }
                }
        }
    }
 }

and this is the contentView that I am trying to navigate to, which keeps resetting back to the root. id like to navigate from here to another, third view.

struct ContentView: View {
    @EnvironmentObject var sheetManager: SheetManager
    @State var privacyPolicyChecked: Bool = false
    @State var bluetoothPolicyChecked: Bool = false
    @State var locationsPolicyChecked: Bool = false
    @State var notificationsPolicyChecked: Bool = false
    @State var apprunningPolicyChecked: Bool = false
    @State var isGoingToNewView = false
    @State var path: [Bool] = []
    private var areAllChecked: Bool {
        return privacyPolicyChecked && bluetoothPolicyChecked && locationsPolicyChecked && notificationsPolicyChecked && apprunningPolicyChecked
    }
    var body: some View {
        NavigationStack(path: $path ) {
            ZStack {
                Color(.gold)
                    .ignoresSafeArea()
                VStack {
                    newContinueNavigationLink
                    Spacer()
                    headerStatement
                    Spacer()
                    VStack {
                        privacyPolicy
                        bluetoothPolicy
                        locationsPolicy
                        notificationsPolicy
                        apprunningPolicy
                    }
                    Spacer()
                    Spacer()
                    continueButton
                }
            }
            .popup(with: sheetManager)
            .navigationDestination(for: Bool.self) { bool in
                SetupGuideSpeakerSearchView()
            }
        }
        .navigationBarBackButtonHidden()
    }
kenada97
  • 19
  • 3

0 Answers0