简体   繁体   English

无法从 Swiftui 中的 Picker 获取选定值

[英]Unable to get selected value from Picker in Swiftui

I am trying to create a model view with some Text and Picker views.我正在尝试使用一些 Text 和 Picker 视图创建一个 model 视图。 Picker view is conditional and toggle based on State variable.选择器视图是有条件的,并且基于 State 变量进行切换。 Toggle is working fine but i am unable to view selected value.切换工作正常,但我无法查看所选值。 I am using simple Picker and unable to see selected values我正在使用简单的 Picker,但无法看到选定的值

Selected value does not show anything when clicked on temperature value.单击温度值时,所选值不显示任何内容。 any idea?任何想法?

   import SwiftUI

struct AddTemperature1: View {
    @Environment(\.presentationMode) var presentationMode
    @State var selection: String = ""
    @State var selectedTemperature : String = ""
    @State var showHideWheel: Bool = false
    
    
    var body: some View {
        
        VStack {
            HStack {
                Text("Temperature")
                    .foregroundColor(Color.gray)
                    .font(.custom("Heavy Condensed", size: 20))
                    .padding()
                
                HStack{
                    Button(action: {
                        self.showHideWheel.toggle()
                    }){
                        Text(selectedTemperature.count > 0 ? selectedTemperature : "Select")
                            .foregroundColor(Color.cclBrandBlueColor)
                            .font(.custom("Heavy Condensed", size: 20))
                        //.padding()
                        Image(systemName: "chevron.right")  .foregroundColor(Color.cclBrandBlueColor)
                            .padding(.trailing)
                    }
                }
                .frame(width: (UIScreen.main.bounds.width - 60) / 2, alignment: .trailing)
                .padding()
            }.background(Color("Color1"))
                .cornerRadius(15)
                .padding(\[.top, .bottom\],10)
                .shadow(color: Color.black.opacity(0.1), radius: 5, x: 8, y: 8)
                .shadow(color: Color.white.opacity(0.5), radius: 5, x: -8, y: -8)
            Spacer()
            if self.showHideWheel {
                VStack (alignment: .center){
                    
                    // VStack{
                    Picker(selection: $selection, label: Text("")){
                        ForEach(50...120, id:\.self){ i in
                            Text("\(i)").tag(i)
                        }
                    }.labelsHidden()
                    Text("You selected: \(selection)")
                    
                }.background(Color("Color1"))
                    .cornerRadius(25)
                    .frame(width: (UIScreen.main.bounds.width - 20), alignment: .center)
                    
                    // .padding(\[.top, .bottom\],10)
                    .shadow(color: Color.black.opacity(0.1), radius: 5, x: 8, y: 8)
                    .shadow(color: Color.white.opacity(0.2), radius: 5, x: -8, y: -8)
            }
        }.frame(width: UIScreen.main.bounds.width)
            .background(Image("background").resizable().scaledToFill().clipped())
        
        
    }
}

struct AddTemperature1_Previews: PreviewProvider {
    static var previews: some View {
        AddTemperature1()
    }
}]

The ForEach list 50...120 consists of Ints, not Strings. ForEach 列表50...120由整数组成,而不是字符串。 So your selection needs to be an Int, not a String.所以你的selection需要是一个 Int,而不是一个 String。 Here's a rewrite of your example that works:这是对您的示例的重写:

struct ContentView: View {
    @State var selection : Int = 50
    var body: some View {
        VStack (alignment: .center){
            Picker("", selection: $selection){
                ForEach(50...120, id:\.self){ i in
                    Text(String(i))
                }
            }.labelsHidden()
            Text("You selected: \(selection)")
        }
    }
}

If you really wanted selection to be a String, your ForEach list would need to be Strings too:如果您真的希望selection是一个字符串,那么您的 ForEach 列表也需要是字符串:

struct ContentView: View {
    @State var selection : String = "50"
    var body: some View {
        VStack (alignment: .center){
            Picker("", selection: $selection){
                ForEach((50...120).map(String.init), id:\.self){ i in
                    Text(i)
                }
            }.labelsHidden()
            Text("You selected: \(selection)")
        }
    }
}

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

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