简体   繁体   中英

TextField Can't input number in ios 14.5

TextField input number but text no display in ios 14.5??? I Have a text to display a num, then touch will show the text field and checkmark to save the num.

it works fine in ios 15.0 but in 14.5 the texts no display number.

struct bodyshape: View {
    @State var wais:Int?
    @State var waisshow = false
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(
        entity: Wais.entity(),
        sortDescriptors: [NSSortDescriptor(keyPath: \Wais.day, ascending: true)],
        animation: .default)
    private var wai: FetchedResults<Wais>
    var body: some View {
        TextField("Num", value: $wais, formatter: NumberFormatter())
            .keyboardType(.numberPad)
        
        Button(action: {waisshow.toggle()
            if waist != nil{
                let newitem = Wais(context: viewContext)
                newitem.day = Date()
                newitem.wais = Int32(wais)
        }
    }
}

here is my text

Text("\(wai.last?.wais ?? 0)" + " cm")
.foregroundColor(.black)
                            .font(.body)
                            .padding(7)
                            .background(waistshow ? Color.gray:Color.white)
                            .clipShape(RoundedRectangle(cornerRadius: 15))
                            .modifier(lessshadow())
                            .position(x: w * 0.82,y: h * 0.38)
                            .onTapGesture {
                                withAnimation {
                                    waisshow.toggle()
                                }
                            }

it works perfectly fine in ios 15.0. then i try tapgesture on text, now it can save the numbers. the problem is the textfield.......

.onTapGesture {
              wais = 5
               withAnimation {
               waisshow.toggle()
                }
                }

Any ideas?

try this in 14.5

TextField("number", text: Binding(
          get: { String(waist ?? 0) },
          set: { waist = Int($0) ?? 0 }
                                ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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