我似乎找不到任何关于这个问题的信息。所以我正在学习SwiftUI,尝试向Form
中添加Picker
,但当点击时该选择器不会显示可用选项。我真的不知道还有什么需要说的了,所以能否有人看一下并帮助我解决此问题?谢谢 :)
//Outside the body code
@State var contacts: [String] = [""]
@State var types: [Type] = [.mobile]
//Inside the body code and inside a Form{}
Section(header: Text("Contact")){
ForEach(0..<types.count, id: \.self) { i in
HStack{
Picker(selection: $types[i], label: Text(""), content: {
Text("Mobile").tag(Type.mobile)
Text("Land Line").tag(Type.landline)
Text("Email").tag(Type.email)
}).pickerStyle(DefaultPickerStyle())
.frame(width: 80.0)
TextField("Insert Detail", text: $contacts[i])
}
}
Button("Add Another") {
types.append(.mobile)
contacts.append("")
}
ForEach(_:content:)
应该仅用于常量数据。相反,应符合Identifiable
数据或使用ForEach(_:id:content:)
并提供显式的id
!这是我测试您的代码时在控制台上打印的消息,很清楚出了什么问题。 - Tushar SharmaForEach(0..<types.count, id:\.self)
。 - Tushar Sharmai
? - Vulps