1

我不明白为什么选择器视图不起作用。我使用了这段代码:

导入 SwiftUI

struct NewChecklistItemView: View {

    var colors = ["Red", "Green", "Blue", "Tartan"]
    @State private var selectedColor = 0


  var checklist: Checklist
  @State var newItemName: String = ""
  @Environment(\.presentationMode) var presentationMode

  var body: some View {
    VStack {
      Text("Add new item")
      Form {
        TextField("Enter new item name here", text: $newItemName)
        Picker(selection: $selectedColor, label: Text("Please choose a color")) {
           ForEach(0 ..< colors.count) { 
              Text(self.colors[$0])
           }
        }
        Text("You selected: \(colors[selectedColor])")

        Button(action: {
        //let newChecklistItem = Category(title: "Category", items: [ChecklistItem(name: self.newItemName)])
            let newItem = ChecklistItem(name: self.newItemName)

            self.checklist.items[0].items.append(newItem)
          self.checklist.printChecklistContents()
          self.presentationMode.wrappedValue.dismiss()
        }) {
          HStack {
            Image(systemName: "plus.circle.fill")
            Text("Add new item")
          }
        }
        .disabled(newItemName.count == 0)

      }
      Text("Swipe down to cancel.")
    }
  }
}

struct NewChecklistItemView_Previews: PreviewProvider {
  static var previews: some View {
    NewChecklistItemView(checklist: Checklist())
  }
}

PickerView 是灰色的并且被禁用。所以我不能选择价值观。可能是什么问题呢?我试图移动pickerView,但没有帮助。

4

1 回答 1

2

要在其中Picker工作,Form您必须将其嵌入NavigationView,例如

NavigationView {
  Text("Add new item")
  Form {
    TextField("Enter new item name here", text: $newItemName)
    Picker(selection: $selectedColor, label: Text("Please choose a color")) {

(当然,这可能需要重新设计/重新布局)......或使用不同的样式选择器样式。

于 2020-10-14T11:44:26.907 回答