繁体   English   中英

从选择器中读取选择 SwiftUI

[英]Reading selection from picker SwiftUI

我正在创建一个选择器,其中从Product类型的数组productList中选择一种食品,我正在尝试创建文本框,其中 output 每个变量都存储在所选产品中。 然而,尽管代码构建成功,选择器不允许用户进行选择。 我怎样才能解决这个问题?

这是我的代码:

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),
    ]
    }
}

选择的类型应该与 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM