【问题标题】:SwiftUI Picker in Form does not show checkmark表单中的 SwiftUI Picker 不显示复选标记
【发布时间】:2020-01-25 22:38:57
【问题描述】:

我在表单中嵌入了一个选取器,但是我无法让它在表单中显示复选标记和选定的值。

NavigationView {
    Form {
        Section {
                Picker(selection: $currencyCode, label: Text("Currency")) {
                    ForEach(0 ..< codes.count) {
                        Text(self.codes[$0]).tag($0)
                    }
                }
            }
    }
}

【问题讨论】:

  • currencyCode的初始值是多少?

标签: ios swiftui


【解决方案1】:

TL;DR

您的变量currencyCodeForEach 中每个元素的ID 类型不匹配。遍历您的 ForEach 中的代码,或者将您的 Picker 传递给索引。

以下是三个等效示例。请注意,传递给 Picker@State 变量始终与 ForEach 迭代的元素的 ID 匹配:

另外请注意,我为@State 变量选择了一个不在数组中的默认值(“”、-1、UUID()),因此在加载表单时不会显示任何内容。如果您需要默认选项,只需将其设为 @State 变量的默认值即可。

示例 1:迭代代码(即字符串)

struct ContentView: View {
    @State private var currencyCode: String = ""
    var codes: [String] = ["EUR", "GBP", "USD"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $currencyCode, label: Text("Currency")) {
                        // ID is a String ----v
                        ForEach(codes, id: \.self) { (string: String) in
                            Text(string)
                        }
                    }
                }
            }
        }
    }
}

示例 2:迭代索引(即 Int)

struct ContentView: View {
    @State private var selectedIndex: Int = -1
    var codes: [String] = ["EUR", "GBP", "USD"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedIndex, label: Text("Currency")) {
                        // ID is an Int --------------v
                        ForEach(codes.indices, id: \.self) { (index: Int) in
                            Text(self.codes[index])
                        }
                    }
                }
            }
        }
    }
}

示例 3:通过 ID 类型(即 UUID)迭代可识别的结构

struct Code: Identifiable {
    var id = UUID()
    var value: String

    init(_ value: String) {
        self.value = value
    }
}

struct ContentView: View {
    @State private var selectedUUID = UUID()
    var codes = [Code("EUR"), Code("GBP"), Code("USD")]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker(selection: $selectedUUID, label: Text("Currency")) {
                        // ID is a UUID, because Code conforms to Identifiable
                        ForEach(self.codes) { (code: Code) in
                            Text(code.value)
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

  • @gpichler:这对你有用吗?我刚刚测试了上面所有 3 个示例,没有一个给我打勾。
  • @GeoffHom 如果您使用的是 XCode 11.1 / iOS 13.1.2,Picker 被一个错误破坏了。它已在 XCode 11.2 / iOS 13.2 测试版中修复。
【解决方案2】:

很难说,你做错了什么,因为你的例子不包括codescurrencyCode 的声明。我怀疑问题出在绑定的类型不同于您在选择器上设置的标签(在您的情况下是 Int )。

无论如何,这行得通:

struct ContentView: View {

    let codes = Array(CurrencyCode.allCases)

    @State private var currencyCode: CurrencyCode?


    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Currency",
                           selection: $currencyCode) {
                            ForEach(codes, id: \.rawValue) {
                                Text($0.rawValue).tag(Optional<CurrencyCode>.some($0))
                            }
                    }
                }
            }
        }

    }
}


enum CurrencyCode: String, CaseIterable {

    case eur = "EUR"
    case gbp = "GBP"
    case usd = "USD"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多