TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

NavigationStack Help

Forums > SwiftUI

Hi, I'm trying to grasp how the new navigation stacks and links work. Before NavigationLinks had an isActive parameter and you could create an emptyview as its label so that if there were some condition to be met, the boolean would be set to true and you would navigate to the next screen. However, I am not able to recreate anything like that. Scenario I have right now: an amition that goes on while an API call is made. Once the animation is done, it sets a boolean value to true (ill call it doneLoading). Before: i would just have an emptyView navigationLink where that bool value would be in the isActive param and once the animation and call were over, i would be brought to the next screen. This time around, i've tried setting the navigationDestination(isPresented: doneLoading){ nextScreen() } but the animation just starts over.

 @State var show = false
    @State var isDone: Bool = false
    @State var doneLoading = false
    @StateObject var viewModel: BumperScreenViewModel
    @StateObject var sheetManager = SheetManager()

    init(viewModel: @autoclosure @escaping () -> BumperScreenViewModel) {
        self._viewModel = .init(wrappedValue: viewModel())
    }

    var body: some View {
        NavigationStack {
            VStack {
                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()
                                    show.toggle()
                                    print("Done")
                                }
                            Spacer()
                        } else {
                            launchAnimation
                        }
                    }
                }
            }
        }
        .navigationDestination(isPresented: $doneLoading) {
            ContentView()
                .environmentObject(sheetManager)
        }
    }

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.