listviewswiftuitabview

SwiftUI TabView set default page on each tap


I'm new to SwiftUI and I'm trying to work out how to set the default page of a TabView that stays constant

My TabView is nested in a ListView and I have set @State private var selection = 1, which works fine when the app is first launched and the TabView opens on page 1 as expected.

However, if the user swipes to a different tab, and then taps the back button to return to the ListView, the next time they open the tab view, it starts on the last tab they were on, rather than tab 1

Here is my ListView that has the TabView nested inside

import SwiftUI

struct Exercises: View {
    
    @Environment(\.managedObjectContext) var managedObjectContext

    var muscleGroup: String
    
    //set initial exercise page to show
    @State private var selection = 1

    @FetchRequest var exercises: FetchedResults<WatchMainExercises>

    init(muscleGroup: String) {
        self.muscleGroup = muscleGroup
        self._exercises = FetchRequest(
            entity: WatchMainExercises.entity(),
            sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)],
            predicate: NSPredicate(format: "\(muscleGroup.lowercased()) == 1")
        )
    }
      
    var body: some View {
        
        
        List(exercises){ exercise in
            
            NavigationLink(destination:
                            
                TabView(selection: $selection) {
                Exercise_Sets(exercise: exercise)
                    .tag(0)
                    .navigationTitle(Text("Close"))
                Exercise_Animation(exercise: exercise)
                    .tag(1)
                    .navigationTitle(Text("Close"))
                Exercise_Info(exercise: exercise)
                    .tag(2)
                    .navigationTitle(Text("Close"))
                }
                .tabViewStyle(.page)
            
                .environment(\.managedObjectContext, self.managedObjectContext)) {
                    
                    ExerciseCell(name: exercise.name!, image: exercise.ensemblesID!, tintColor: DataHelper.checkDifficulty(record: exercise), exercise: exercise)
                       
                }
                .navigationTitle("Exercises")
                .navigationBarTitleDisplayMode(.inline)
              
          
        }
    }
}

currently the Tab Pages are just blank structs that take an exercise object as an arg

How Would I go about setting the initial TabView to page 1 every time the user taps back from the TabView, or is there a way of setting a constant initial tab page

I've spent ages searching for similar problems, but can't find anything that resolves the issue

Any help would be greatly appreciated

Thanks in advance


Solution

  • You could call onAppear on your TabView to set the desired tag each time the view appears.

    TabView(selection: $selection) {
        // your pages...
    }
    .onAppear {
        selection = 1
    }