SwiftUI-选择器

SwiftUI - Picker

我正在学习swift,现在我已经插入了 2 个调用 struct 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()
    }
}

使用filter从第一个Picker开始过滤掉所选元素,除非所选大小写为selectCase。也为您做了一些改进。

  1. 由于枚举已经符合 CaseIterable 协议,因此您不必创建自定义 animals 数组。
  2. 此外,您可以在 rawValue 上使用 capitalized,而不是为感觉多余的每个枚举案例提供 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 { [=10=] != firstAnimal || firstAnimal == .select }, id: \.self) { element2 in
                                Text(element2.rawValue.capitalized)
                            }
                        }
                }.font(.system(size: 15))
            }.navigationBarTitle("List", displayMode: .inline)
        }
    }
}