SwiftUI:如何使用 SegmentedPickerStyle() return 与 Picker 的相同索引
SwiftUI: How do I return to same index with a Picker using SegmentedPickerStyle()
这只是我在思考 ViewBuilder 和新的 SwiftUI 范例。
我的屏幕顶部有一个 "menu" 以及几个按钮。
如果我进行搜索(点击放大镜),当我 return 时,索引总是 return 为 0,第一项是 selected/displayed。我想 return 到调用时的相同索引。我如何记住索引并重置它?
这是主菜单:
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[[=11=]]).tag([=11=])
}
}
.pickerStyle(SegmentedPickerStyle())
}
SecondLevelView(continentIndex: continentIndex)
.modifier(SystemServices())
}
}
}
通常我会编写 UIKit 代码来保存索引并恢复它,但我不确定这样的代码会去哪里,因为 ViewBuilder 不与内联代码合作。什么是公认的做法?
我决定使用 @AppStorage
属性 包装器。
由于选择器选项对应于枚举的原始值,我向枚举 (ContinentType) 添加了一个键:
static var key = "ContinentIndex"
然后在我替换的视图中:
@State private var continentIndex = 0
与:
@AppStorage(ContinentType.key) var selectedContinentIndex = 0
这会记住最后选择的索引,所以我可以导航到其他游戏模式,但是当我 return 到这个视图时,它会记住我正在使用哪个大陆。
这是上下文更新:
Section {
Picker(selection: $selectedContinentIndex, label: Text("Continent")) {
ForEach(0 ..< 5) {
Text(ContinentType.continents[[=13=]]).tag([=13=])
}
}
.pickerStyle(SegmentedPickerStyle())
}
这只是我在思考 ViewBuilder 和新的 SwiftUI 范例。
我的屏幕顶部有一个 "menu" 以及几个按钮。
如果我进行搜索(点击放大镜),当我 return 时,索引总是 return 为 0,第一项是 selected/displayed。我想 return 到调用时的相同索引。我如何记住索引并重置它?
这是主菜单:
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[[=11=]]).tag([=11=])
}
}
.pickerStyle(SegmentedPickerStyle())
}
SecondLevelView(continentIndex: continentIndex)
.modifier(SystemServices())
}
}
}
通常我会编写 UIKit 代码来保存索引并恢复它,但我不确定这样的代码会去哪里,因为 ViewBuilder 不与内联代码合作。什么是公认的做法?
我决定使用 @AppStorage
属性 包装器。
由于选择器选项对应于枚举的原始值,我向枚举 (ContinentType) 添加了一个键:
static var key = "ContinentIndex"
然后在我替换的视图中:
@State private var continentIndex = 0
与:
@AppStorage(ContinentType.key) var selectedContinentIndex = 0
这会记住最后选择的索引,所以我可以导航到其他游戏模式,但是当我 return 到这个视图时,它会记住我正在使用哪个大陆。
这是上下文更新:
Section {
Picker(selection: $selectedContinentIndex, label: Text("Continent")) {
ForEach(0 ..< 5) {
Text(ContinentType.continents[[=13=]]).tag([=13=])
}
}
.pickerStyle(SegmentedPickerStyle())
}