SwiftUI Picker不再适用于Xcode 11 beta 7

时间:2019-09-09 13:12:37

标签: ios swiftui

我正在使用选择器让用户选择字体类型。该代码曾经可以工作,但不再正常工作。我认为在更新到Xcode 11 beta 7后,它可能已经停止工作。我可以点击Picker来显示Picker选项列表,但是我先前的选择不再在其旁边显示对勾,并且不再点击选项什么都做。我是在做错什么还是仅仅是Beta错误?

SettingsFont视图:

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

}

字体名称:

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

1 个答案:

答案 0 :(得分:0)

您需要将视图包装在导航视图中

OnBoardingViewModel

需要导航视图才能导航到带有选项的屏幕,然后向后导航。