【发布时间】:2020-09-13 18:40:17
【问题描述】:
我是 SwiftUI 的新手,想从选择器中保存用户选择。我知道我需要 UserDefaults,但我不知道在这种情况下如何使用 UserDefaults。
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
@State var PickerSelection = 0
//PickerStyle
init() {
UISegmentedControl.appearance().selectedSegmentTintColor = .init(UIColor(named: "Color_Picker")!)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.init(named: "Color_Picker")!], for: .normal)
}
var body: some View {
VStack {
Picker("", selection: $PickerSelection) {
Text("Selection 1").tag(0)
Text("Selection 2").tag(1)
} .pickerStyle(SegmentedPickerStyle()).padding(.horizontal, 100)
Text("Hello World!")
}
}
【问题讨论】: