我不明白为什么选择器视图不起作用。我使用了这段代码:
导入 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,但没有帮助。