繁体   English   中英

SwiftUI 带 Int16 的选择器

[英]SwiftUI Picker with Int16

在核心数据内部,我使用的是 Int16,但是使用带有 Int16 的选择器不起作用。 有谁知道为什么? 我正在使用的解决方法是创建一个 model 来管理 Int 中的值,在保存期间,我转换 Int16 中的值。

struct PikHeiVIEW: View {
    @Binding var height:Int
    @Binding var heightInt16:Int16
    
    var body: some View {
        VStack {
            Picker("", selection: $height) {
                ForEach(150...190, id: \.self) {
                    Text("\($0)")
                }
            }
            Text("You selected: \(height)")
            Picker("", selection: $heightInt16) {
                ForEach(150...190, id: \.self) {
                    Text("\($0)")
                }
            }
            Text("You selected: \(heightInt16)")
        }
    }
}

struct TESTVIEW: View {
        
    @State private var height:Int = 180
    @State private var heightInt16:Int16 = 180
    
    var body: some View {
        PikHeiVIEW(height: $height, heightInt16: $heightInt16)
    }
}
struct TESTVIEW_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
            TESTVIEW()
        }
    }
}

您可以使用Int16创建选择器范围

Picker("", selection: $heightInt16) {
    ForEach(Int16(150)...Int16(190), id: \.self) {
        Text("\($0)")
    }
}

暂无
暂无

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

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