[英]SwiftUI picker in Form not working properly
I'm struggling with this weird Picker behavior.我正在为这种奇怪的 Picker 行为而苦苦挣扎。 Basically, when I tap on the picker row I can see the options, but every time I tap on a row, nothing happens.基本上,当我点击选择器行时,我可以看到选项,但每次点击一行时,什么都没有发生。 No checkmark, no coming back to previous view and no selection.没有复选标记,没有回到上一个视图,也没有选择。 I tried also adding id: \.self
in the ForEach
, but nothing changes.我还尝试在ForEach
中添加id: \.self
,但没有任何变化。 I'm using iOS 15 with Xcode 13, but the problem is present also on iOS 14 and Xcode 12.x.我将 iOS 15 与 Xcode 13 一起使用,但问题也存在于 iOS 14 和 Xcode 12.x 上。 Here's the code of my view:这是我的观点的代码:
@State private var month: Month?
@State private var category: Category?
var body: some View {
NavigationView {
VStack {
Form {
Section {
Picker(month?.name ?? "Seleziona il mese", selection: $month) {
ForEach(model.months) { month in
Text(month.name).tag(month)
}
}
Picker(category?.name ?? "Seleziona la categoria", selection: $category) {
ForEach(model.categories) { category in
Text(category.name).tag(category)
}
}
}
}
Spacer()
}
.navigationTitle("Aggiungi Spesa")
}
}
Here's the code of Month
( Category
is basically the same):这是Month
的代码( Category
基本相同):
class Month: Decodable, AirtableRecord, Identifiable {
var id: String
var createdTime: String
var number: Int
var name: String
var completeName: String
init(
id: String,
createdTime: String,
number: Int,
name: String,
completeName: String) {
self.id = id
self.createdTime = createdTime
self.number = number
self.name = name
self.completeName = completeName
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
createdTime = try container.decode(String.self, forKey: .createdTime)
let fields = try container.nestedContainer(keyedBy: FieldsKeys.self, forKey: .fields)
name = try fields.decode(String.self, forKey: .name)
number = try fields.decode(Int.self, forKey: .number)
completeName = try fields.decode(String.self, forKey: .completeName)
}
enum CodingKeys: String, CodingKey {
case id
case createdTime
case fields
}
enum FieldsKeys: String, CodingKey {
case number = "Numero"
case name = "Nome"
case completeName = "Key"
}
}
extension Month: Equatable {
static func == (lhs: Month, rhs: Month) -> Bool {
return lhs.id == rhs.id
}
}
extension Month: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
extension Month: CustomStringConvertible {
var description: String {
return "\(completeName)"
}
}
This happens with optionals quite a bit you just have to specify type这发生在可选项上,你只需要指定类型
Change your tags to something like this把你的标签改成这样
.tag(month as? Month)
.tag(category as? Category)
The types have to match exactly.类型必须完全匹配。 month
and category
are non-optionals so they need to be cast to optionals since your @State
is an optional month
和category
是非可选的,因此它们需要转换为可选,因为您的@State
是可选的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.