我是 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 頁,或者有沒有辦法設定一個恒定的初始選項卡頁
我花了很長時間尋找類似的問題,但找不到任何解決問題的方法
任何幫助將不勝感激
提前致謝
uj5u.com熱心網友回復:
您可以在 TabView 上呼叫 onAppear 以在每次視圖出現時設定所需的標簽。
TabView(selection: $selection) {
// your pages...
}
.onAppear {
selection = 1
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/458987.html
下一篇:在matlab中標記x軸上的點
