gpt4 book ai didi

ios - SwiftUI Picker 在 Xcode 11 beta 7 中不再工作

转载 作者:行者123 更新时间:2023-11-29 05:28:58 25 4
gpt4 key购买 nike

我正在使用选择器让用户选择字体类型。该代码曾经可以正常工作,但现在不再正常工作。我认为在更新到 Xcode 11 beta 7 后它可能已停止工作。我可以点击选取器以调出选取器选项列表,但我之前的选择旁边不再显示复选标记,并且点击选项不再做任何事。我做错了什么还是这只是一个测试错误?

设置字体 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"))
}

}

用户设置:

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)
}

}

字体名称:

enum FontName: String, CaseIterable {
case sanFrancisco = "San Francisco"
case bitter = "Bitter"
case helvetica = "Helvetica"
case georgia = "Georgia"
case avenir = "Avenir"
case menlo = "Menlo"
}

Screenshot of my iPhone app with Picker

最佳答案

您需要将 View 包装在导航 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"))
}

}

需要导航 View 来导航到带有选项的屏幕,然后导航回来。

关于ios - SwiftUI Picker 在 Xcode 11 beta 7 中不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com