从选择器 SwiftUI 中读取选择

Posted

技术标签:

【中文标题】从选择器 SwiftUI 中读取选择【英文标题】:Reading selection from picker SwiftUI 【发布时间】:2020-10-24 02:13:04 【问题描述】:

我正在创建一个选择器,其中从 productList 类型的 Product 类型的数组中选择食品,并且我正在尝试创建输出存储在所选产品中的每个变量的文本框。然而,尽管代码已成功构建,选择器将不允许用户进行选择。我该如何解决这个问题?

这是我的代码:

struct AddView : View 
    @State var selectFood = 0
    @ObservedObject var database : Database
    var body: some View 
        VStack
            Picker(selection: $selectFood, label: Text("What did you eat?")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white))
            
                ForEach(database.productList)  (productList: Product) in
                    Text(productList.name)
                
            
            .pickerStyle(MenuPickerStyle())
            
            Spacer(minLength: 100)
            
            ZStack 
                Rectangle()
                    .frame(width: 350, height: 90, alignment: .center)
                    .foregroundColor(.white)
                    .opacity(0.25)
                    .offset(y: 40)
                    .cornerRadius(10)
                Text("Calories: \(database.productList[selectFood].calories)")
                
            
            
            Spacer()
            
            Text("Enter")
            
            Spacer()
        
        .padding(.horizontal, 50)
    

这是存储数组的代码:

struct Product : Identifiable 
    var id = UUID()
    var name : String
    var calories : Double
    var protein : Double
    var carbs : Double
    var fats : Double
    var weight : Double

    


class Database : ObservableObject
    
    @Published var productList = [Product]()
    
    init() 
        self.productList = [
            Product(name: "Apple", calories: 52, protein: 0.3, carbs: 14, fats: 0.2, weight: 80),
            Product(name: "Avocado", calories: 160, protein: 2, carbs: 9, fats: 15, weight: 600),
            Product(name: "Banana", calories: 89, protein: 1.1, carbs: 23, fats: 0.3, weight: 120),
            Product(name: "Broccoli", calories: 34, protein: 2.8, carbs: 5, fats: 0.4, weight: 225),
            Product(name: "Burger", calories: 264, protein: 13, carbs: 30, fats: 10, weight: 110),
            Product(name: "Carrot", calories: 41, protein: 0.9, carbs: 10, fats: 0.2, weight: 61),
            Product(name: "Cheerios", calories: 379, protein: 6, carbs: 83, fats: 5, weight: 40),
            Product(name: "Cherry", calories: 50, protein: 1, carbs: 12, fats: 0.3, weight: 5),
            Product(name: "Chicken Nuggets", calories: 302, protein: 16, carbs: 15, fats: 20, weight: 16),
            Product(name: "Cocoa Pops", calories: 389, protein: 5, carbs: 85, fats: 2.9, weight: 45),
            Product(name: "Corn Flakes", calories: 357, protein: 8, carbs: 84, fats: 0.4, weight: 45),
            Product(name: "Crunchy Nuts", calories: 402, protein: 6, carbs: 82, fats: 5, weight: 45),
            Product(name: "Eggs", calories: 196, protein: 14, carbs: 0.8, fats: 15, weight: 60),
            Product(name: "Fries", calories: 312, protein: 3.4, carbs: 43, fats: 15, weight: 100),
            Product(name: "Froasted Flakes", calories: 369, protein: 4, carbs: 89, fats: 1.7, weight: 45),
    ]
    

【问题讨论】:

【参考方案1】:

选择的类型应该和 Picker 数据块 id 或标签的类型相同,所以如果你想要选择索引,那么它应该是这样的

struct AddView : View 
    @State var selectFood = 0
    @ObservedObject var database : Database

    var body: some View 
        VStack
            Picker(selection: $selectFood, label: Text("What did you eat?")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(.white))
            
                ForEach(database.productList.indices, id: \.self)  i in  // << here
                    Text(database.productList[i].name)    // << and here 
                
            
            .pickerStyle(MenuPickerStyle())

  // ... other code

【讨论】:

以上是关于从选择器 SwiftUI 中读取选择的主要内容,如果未能解决你的问题,请参考以下文章

来自先前选择器的选择器值 - CoreData/SwiftUI

从 json 填充选择器,SwiftUI

SwiftUI 从 CoreData 设置初始选择器值

swiftUI 中的选取器正在从 Firestore 中选择文档 ID 而不是文档字段

SwiftUI 从选择器的字符串结果中设置绑定

应用程序在从表单(SwiftUI)的选择器上选择时扩展“其他”类别)