这只是我对 ViewBuilders 和新的 SwiftUI 范式的理解。
我的屏幕顶部有一个“菜单”和几个按钮。
如果我进行搜索(点击放大镜),当我返回时索引总是返回到 0 并且第一个项目被选中/显示。我想回到被叫走时的同一个索引。我如何记住索引并重置它?
这是主菜单:
struct TopLevelMenu: View {
/// Toggle to display the Game State (Map) on button tap
@State private var shouldShowWorldMap = false
var body: some View {
NavigationView {
VStack {
if shouldShowWorldMap {
ZStack {
AnimatedSequence()
.modifier(SystemServices())
} else {
TopLevelView()
}
}
}
}
}
struct TopLevelView: View {
/// Tracks the sheet presentation and current play environment (continent)
/// mapCenter, display flags, gameOver flat, current continent
var gameState: GameState = GameState.shared
/// Handles the game logic, turns, scoring, etc.
var gameManager: GameManager = GameManager.shared
/// current game modes: continent, country, capital, grid, about
@State private var continentIndex = 0
/// If Help or the World Map is not displayed (triggered by buttons at the top), then this is the Main Display after launch
var body: some View {
VStack {
Section {
Picker(selection: $continentIndex, label: Text("N/A")) {
ForEach(0 ..< 5) {
Text(Continent.continents[$0]).tag($0)
}
}
.pickerStyle(SegmentedPickerStyle())
}
SecondLevelView(continentIndex: continentIndex)
.modifier(SystemServices())
}
}
}
通常我会编写 UIKit 代码来保存索引并恢复它,但我不确定这样的代码会去哪里,因为 ViewBuilders 不与内联代码合作。什么是公认的做法?