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

标签 ios swiftui

我正在使用选择器让用户选择字体类型。该代码曾经可以正常工作,但现在不再正常工作。我认为在更新到 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/

相关文章:

iphone - 为什么即使我已经越狱,iOS 6.1.1中的SandBox对于App仍然存在?

ios - GoogleMaps iOS - 获取直线而不是路线

ios - Xamarin iOS 应用程序检查 GPS 是否已启用或提示用户

ios - 如何在 Xcode6 中为新创建的约束禁用 "Relative to margin"选项

ios - 将固定大小设置为图像

ios - Facebook ID 字符串长度

SwiftUI iOS15 Binding ForEach 正在取消 TextField KeyBoard

swiftui - SwiftUI 中的 WKWebView 未在 macOS 上加载 HTML 字符串

ios - SwiftUI - 动态刷新 UINavigationBar.appearance().backgroundColor

SwiftUI withAnimation 导致意外行为