【问题标题】:SwiftUI - PickerSwiftUI - 选择器
【发布时间】:2020-07-18 07:56:11
【问题描述】:

我正在快速学习,现在我已经插入了 2 个调用结构 Animal 的选择器。 我无法理解的是如何告诉 swift 如果第一个选择器选择了一个枚举值,那么第二个选择器可用的枚举中不能存在相同的值,因为它已经被选择了。

非常感谢:)

import SwiftUI

enum Animal: String, CaseIterable {
    
    case selectCase = "Select"
    case bear = "Bear"
    case cat = "Cat"
    case dog = "Dog"
    case lion = "Lion"
    case tiger = "Tiger"
    
    
    static var animals: [String] = [selectCase.rawValue, bear.rawValue, cat.rawValue, dog.rawValue, lion.rawValue, tiger.rawValue]
}


struct ContentView: View {
    
    @State private var Picker1: String = Animal.animals[0]
    @State private var Picker2: String = Animal.animals[0]
    
    
    var body: some View {
        
        NavigationView {
            
            Form {
                Section(header: Text("Animals")
                    .foregroundColor(.black)
                    .font(.system(size: 15))
                    .fontWeight(.bold)) {
                        
                        Picker(selection: $Picker1, label: Text("Select first animal")) {
                            
                            ForEach(Animal.animals, id: \.self) { element in
                                Text(element)
                            }
                        }
                        
                        Picker(selection: $Picker2, label: Text("Select second animal")) {
                            
                            ForEach(Animal.animals, id: \.self) { element2 in
                                Text(element2)
                                
                            }
                        }
                        
                }.font(.system(size: 15))
            }.navigationBarTitle("List", displayMode: .inline)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

【问题讨论】:

    标签: enums swiftui picker


    【解决方案1】:

    使用filter 从第一个Picker 中过滤掉选定的元素,除非选定的案例是selectCase。还为您做了一些改进。

    1. 由于枚举已经符合CaseIterable 协议,您不必创建自定义animals 数组。
    2. 此外,您可以在 rawValue 上使用大写字母,而不是为每个感觉多余的枚举案例提供 rawValue。
    3. 属性名称应以小写字母开头并命名为Picker1 以免混淆,将所选动物命名为firstAnimalselectedFirstAnimal 将来会更方便。

    这是代码。

    enum Animal: String, CaseIterable {
        case select
        case bear
        case cat
        case dog
        case lion
        case tiger
    }
    
    struct ContentView: View {
    
        @State private var firstAnimal = Animal.allCases[0]
        @State private var secondAnimal = Animal.allCases[0]
    
        var body: some View {
            NavigationView {
                Form {
                    Section(header: Text("Animals")
                        .foregroundColor(.black)
                        .font(.system(size: 15))
                        .fontWeight(.bold)) {
                            Picker(selection: $firstAnimal, label: Text("Select first animal")) {
                                ForEach(Animal.allCases, id: \.self) { element in
                                    Text(element.rawValue.capitalized)
                                }
                            }
                            Picker(selection: $secondAnimal, label: Text("Select second animal")) {
                                ForEach(Animal.allCases.filter { $0 != firstAnimal || firstAnimal == .select }, id: \.self) { element2 in
                                    Text(element2.rawValue.capitalized)
                                }
                            }
                    }.font(.system(size: 15))
                }.navigationBarTitle("List", displayMode: .inline)
            }
        }
    }
    

    【讨论】:

    • 哇。我真的非常感谢你,你解决了我的问题。 :) 祝你有美好的一天再次感谢
    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2021-02-07
    • 2021-12-25
    相关资源
    最近更新 更多