SwiftUI TabView 在每次点击时设置默认页面

问题描述 投票:0回答:2

我是 SwiftUI 新手,我正在尝试研究如何设置保持不变的 TabView 的默认页面

我的 TabView 嵌套在 ListView 中,并且我设置了

@State private var selection = 1
,当应用程序首次启动并且 TabView 按预期在第 1 页打开时,效果很好。

但是,如果用户滑动到不同的选项卡,然后点击后退按钮返回到 ListView,则下次打开选项卡视图时,它将从他们所在的最后一个选项卡开始,而不是选项卡 1

这是我的 ListView,其中嵌套了 TabView

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)
              
          
        }
    }
}

目前选项卡页只是空白结构,将练习对象作为参数

每次用户从 TabView 轻按时,我该如何将初始 TabView 设置为第 1 页,或者是否有办法设置恒定的初始选项卡页面

我花了很长时间寻找类似的问题,但找不到任何解决问题的方法

任何帮助将不胜感激

提前致谢

listview swiftui tabview
2个回答
2
投票

您可以在 TabView 上调用 onAppear 来在每次视图出现时设置所需的标签。

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

0
投票

为了避免每次用户返回此屏幕时(例如弹出导航时)“寻呼机”都会重置,您应该考虑使用任务。另外,您需要标签(如果您使用滚动视图,则需要带有滚动视图阅读器的 id):

struct DetailView: View {

    @State var selection: Int = 0
    var selectedIndex: Int = 0

    var body: some View {
        TabView(selection: $selection) {
            Text("First view")
            .tag(1)
            Text("Second view")
            .tag(2)
            Text("Third view")
            .tag(3)
            Text("Fourth view")
            .tag(4)
        }
        .tabViewStyle(PageTabViewStyle())
        .task {
            selection = selectedIndex
        }
    }
}

像这样使用它:

PagerView(selectedIndex: 2)
© www.soinside.com 2019 - 2024. All rights reserved.