ios - 表单中的 SwiftUI 选择器不显示复选标记

标签 ios swiftui

我在 Form 中嵌入了一个 Picker,但是我无法让它工作,因为它在表单中显示了一个复选标记和选定的值。

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

No checkmark and no selected value

最佳答案

TL;博士

您的变量 currencyCode与您 ForEach 中每个元素的 ID 类型不匹配.要么遍历您的 ForEach 中的代码,或通过您的 Picker一个索引。

下面是三个等效的例子。请注意 @State传递给 Picker 的变量始终匹配 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)
                        }
                    }
                }
            }
        }
    }
}

关于ios - 表单中的 SwiftUI 选择器不显示复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58103437/

相关文章:

iphone - viewForZoomingInScrollView UIScrollView 背景和图像不同步

ios - 如何使用 SwiftUI 处理屏幕触摸事件?

swift - SwiftUI VStack和列表中其他 View 压缩的 View

swift - 异步搜索栏

iOS 9.2 UIDatePicker 角和边框不工作

ios - 屏幕比例/大小在 iOS 8 中是正确的,但在同一设备上的 iOS 9 中是错误的

使用 MVVM 模式显示数组中的值的 SwiftUI 问题

ios - 为什么在 TabView 上显示工作表会更改我选择的选项卡?

ios - Mkmapview 中两个位置之间的多条路线

ios - Firebase 电话身份验证失败