UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

When Sheet is dismissed, screen returns to wrong View.

Forums > SwiftUI

Hello,

I'm currently facing some weird behavior that i can't seem to solve by myself. I have hierarchical structure where user can select one specific workout and see list of exercises. Then, he can click on exercise and see how many sets and reps he have to complete.

User can add new Workout with toolbar button + sheet in WorkoutListView (all works fine). User can also add new Exercise with toolbar button + sheet in ExerciseListView (one step deeper, but all works fine). Then, when user is inside the Exercise and he wants to add Sets & reps, with toolbar button + sheet, when this sheet is dismissed, user is returned not to SetsListView, but one step back - to ExerciseListView. And then when he enters the SetsListView the data is there.

WorkoutListView -> click on workout -> ExerciseListView -> click on exercise -> SetsListView

struct WorkoutListView: View {
    @EnvironmentObject var viewModel: ViewModel
    @State var showAddWorkoutView = false

    var body: some View {
        NavigationView {
            List {
                ForEach(viewModel.workouts) { workout in
                    NavigationLink(destination: ExerciseListView(selectedWorkout: workout)) {
                        Text(workout.title)
                    }
                } // ForEach
            } // List
            .navigationTitle("Workouts")
            .toolbar {
                ToolbarItem {
                    Button {
                        showAddWorkoutView = true
                    } label: {
                        Image(systemName: "plus")
                    }
                }
            }
            .sheet(isPresented: $showAddWorkoutView) {
                AddWorkoutView()
            }
        } // NavigationView
    } // Body
}
struct ExerciseListView: View {
    @EnvironmentObject var viewModel: ViewModel
    var selectedWorkout: Workout

    @State var showAddExerciseView = false

    var body: some View {
        VStack {
            List {
                ForEach(selectedWorkout.exercises) { exercise in
                    NavigationLink(destination: SetsListView(selectedWorkout: selectedWorkout, selectedExercise: exercise)) {
                        Text(exercise.title)
                    }
                }
            }
        }
        .navigationTitle(selectedWorkout.title)
        .toolbar {
            ToolbarItem {
                Button {
                    showAddExerciseView = true
                } label: {
                    Image(systemName: "plus")
                }

            }
        }
        .sheet(isPresented: $showAddExerciseView) {
            AddExerciseView(selectedWorkout: selectedWorkout)
        }
    }
}
struct SetsListView: View {
    @EnvironmentObject var viewModel: ViewModel
    var selectedWorkout: Workout
    var selectedExercise: Exercise

    @State var showAddSetView = false

    var body: some View {
        List {
            ForEach(selectedExercise.sets) { currentSet in
                Text(currentSet.title)
            }
        }
        .navigationTitle(selectedExercise.title)
        .toolbar {
            ToolbarItem {
                Button {
                    showAddSetView = true
                } label: {
                    Text("Add")
                }

            }
        }
        .sheet(isPresented: $showAddSetView) {
            AddSetView(selectedWorkout: selectedWorkout, selectedExercise: selectedExercise)
        }
    }
}

Can someone explain why i'm getting redirected back to ExerciseListView and not SetsListView from where the sheet was presented? And maybe someone has an idea how to fix this.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.