简体   繁体   English

SwiftUI:选择器不可点击

[英]SwiftUI: Picker not clickable

I have inserted a Picker inside a Form and wrapped it with a NavigationView .我在Form中插入了一个Picker并用NavigationView包裹它。 However, the Picker won't display the options.但是,选择器不会显示选项。 It seems like it clicks (it gets highlighted when I press on it, though in a funky way).它看起来像是咔哒一声(当我按下它时它会突出显示,尽管是以一种时髦的方式)。 I tried using the same code on a separate View and it worked.我尝试在单独的View上使用相同的代码并且它有效。 For context, this View is a sheet.对于上下文,此View是一张工作表。 I tried switching it to a normal View but it still doesn't work.我尝试将其切换为普通View ,但仍然无法正常工作。

@State private var selectedSupplier = 1
var suppliers = ["ABB", "Schneider", "Kahane"]

var body: some View {
    VStack {
        NavigationView {
            Form {
                HStack {
                    Text("פריט אינו קיים במלאי")
                }
                
                // section
                // item details
                Section(header: Text("הקלד פרטים")) {
                    HStack {
                        Text("חברה")
                        Spacer()
                        TextField("חברה", text: $brand)
                            .fixedSize()
                    }
                    
                    HStack {
                        Text("סוג פריט")
                        Spacer()
                        TextField("סוג פריט", text: $type)
                            .fixedSize()
                    }
                    
                    HStack {
                        Text("כינוי")
                        Spacer()
                        TextField("כינוי", text: $nickname)
                            .fixedSize()
                    }
                    
                    HStack {
                        Picker(selection: $selectedSupplier, label: Text("ספק")) {
                            //                                ForEach(0 ..<suppliers.count) { index in
                            //                                    Text(self.suppliers[index]).tag(index)
                            //                                }
                            Text("ABB").tag(0)
                            Text("Schneider").tag(1)
                            Text("Kahane").tag(2)
                        }
                    }
                    
                    HStack {
                        Text("ספק")
                        Spacer()
                        TextField("ספק", text: $supplier)
                            .fixedSize()
                    }
                    
                    HStack {
                        // !! should create a dropdown menu
                        Text("כמות במלאי")
                        Spacer()
                        TextField("כמות במלאי", text: $stock)
                            .keyboardType(.decimalPad)
                            .fixedSize()
                    }
                    
                    HStack {
                        // !! should create a dropdown menu
                        Text("כמות מומלצת")
                        Spacer()
                        TextField("כמות מומלצת", text: $recQuantity)
                            .keyboardType(.decimalPad)
                            .fixedSize()
                    }
                }

                Section {
                    Picker("Choose a number", selection: $selectedSupplier) {
                        ForEach(0 ..< suppliers.count, id: \.self) { index in
                                            Text(suppliers[index])
                                        }
                                    }
                }
                // section
                // button section
                Section {
                    Button(action: {
                        
                        // Call add data
                        model.addData(id: model.barcodeValue, brand: brand, type: type, stock: Int(stock) ?? 0, nickname: nickname, supplier: supplier, recQuantity: Int(recQuantity) ?? 0)
                        
                        // Clear the text fields
                        brand = ""
                        type = ""
                        stock = ""
                        nickname = ""
                        supplier = ""
                        recQuantity = ""
                        
                        // close down window - return to Scanner View
                        showReg.toggle()
                        
                        
                    }, label: {
                        HStack {
                            Spacer()
                            Text("הוסיף")
                            Spacer()
                        }
                    })
                        .buttonStyle(PlainButtonStyle())
                        .foregroundColor(Color(UIColor.systemBlue))
                    // if any of the three entries (except nickname) is empty, disable button
                        .disabled(type.isEmpty || brand.isEmpty || stock.isEmpty)
                }
            }
            .navigationBarTitle("הוספת פריט למלאי")
            // hide the keyboard if user clicks outside the form
            .onTapGesture {
                hideKeyboard()
            }
            .environment(\.layoutDirection, .rightToLeft)
            }
        }
    }
}

I had a similar issue where my picker suddenly decided to stop working.我有一个类似的问题,我的选择器突然决定停止工作。 I was at a loss, until I realized that it was the.onTapGesture that I added to resign the keyboard.我一头雾水,直到我意识到这是我添加的用于退出键盘的.onTapGesture。

VStack {
    ...
    Picker() {
        ...
    }
    
    HStack {
        Text()
        TextField()
    }
    //Placing the .onTapGesture here doesn't interfere with the Picker, but it doesn't get the functionality you'd like, either
}
//The code below interferes with the ability to tap on the Picker
.onTapGesture {
_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}


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

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