[英]SwiftUI Picker no longer works in Xcode 11 beta 7
I'm using a Picker to have the user select a font type. 我正在使用Picker让用户选择字体类型。 This code used to work, but it no longer works correctly.
该代码曾经可以工作,但不再正常工作。 I think it may have stopped working after I updated to Xcode 11 beta 7. I can tap on the Picker to bring up the Picker options list, but my previous selection no longer shows a checkmark next to it, and tapping on an option no longer does anything.
我认为在更新到Xcode 11 beta 7之后,它可能已经停止工作。我可以点击Picker来显示Picker选项列表,但是我先前的选择不再在其旁边显示对勾,并且不再点击选项什么都做。 Am I doing something wrong or is this just a beta bug?
我是在做错什么还是仅仅是beta错误?
SettingsFont view: 设置字体视图:
struct SettingsFont: View {
@EnvironmentObject var userSettings: UserSettings
let fontNames = FontName.allCases
var body: some View {
Form {
Section {
Picker(selection: $userSettings.fontName, label: Text("Font Type")) {
ForEach(fontNames, id: \.self) { fontName in
Text(fontName.rawValue).tag(fontName.rawValue)
.font(FontFactory.noteFont(for: fontName))
}
}
}
}
.listStyle(GroupedListStyle())
.navigationBarTitle(Text("Font"))
}
}
UserSettings: 用户设置:
import Foundation
import Combine
class UserSettings: ObservableObject {
var objectWillChange = PassthroughSubject<Void, Never>()
enum StringSettingsKey: String {
case fontName
}
struct Default {
static let fontName = FontName.sanFrancisco
}
@Published var fontName: FontName = Default.fontName {
willSet { objectWillChange.send() }
didSet { set(string: fontName.rawValue, for: .fontName) }
}
init() {
setFromUserDefaults(settingsKey: .fontName)
}
private func setFromUserDefaults(settingsKey: StringSettingsKey) {
if let storedString = UserDefaults.standard.string(forKey: settingsKey.rawValue) {
switch settingsKey {
case .fontName: fontName = FontName(rawValue: storedString) ?? Default.fontName
}
return
}
}
private func set(string: String, for settingsKey: StringSettingsKey) {
UserDefaults.standard.set(string, forKey: settingsKey.rawValue)
}
private func storedString(for settingsKey: StringSettingsKey) -> String? {
return UserDefaults.standard.string(forKey: settingsKey.rawValue)
}
}
FontName: 字体名称:
enum FontName: String, CaseIterable {
case sanFrancisco = "San Francisco"
case bitter = "Bitter"
case helvetica = "Helvetica"
case georgia = "Georgia"
case avenir = "Avenir"
case menlo = "Menlo"
}
You need to wrap your view in a navigation view: 您需要将视图包装在导航视图中:
struct SettingsFont: View {
@EnvironmentObject var userSettings: UserSettings
let fontNames = FontName.allCases
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $userSettings.fontName, label: Text("Font Type")) {
ForEach(fontNames, id: \.self) { fontName in
Text(fontName.rawValue).tag(fontName.rawValue)
.font(FontFactory.noteFont(for: fontName))
}
}
}
}
}
.navigationBarTitle(Text("Font"))
}
}
The navigation view is needed to navigate to the screen with options and then to navigate back. 需要导航视图才能导航到带有选项的屏幕,然后向后导航。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.