0

好吧,我是 SwiftUI 的新手,我找到了一个代码,只需将其放在 //NavigationView 中的大括号之后,然后在我开始滚动时关闭键盘。很好但是我想在我的应用程序中选择百分比时激活它. 这是我的代码:谢谢你帮助我。

好吧,我是 SwiftUI 的新手,我找到了一个代码,只需将其放在 //NavigationView 中的大括号之后,然后在我开始滚动时关闭键盘。很好但是我想在我的应用程序中选择百分比时激活它. 这是我的代码:谢谢你帮助我。


struct ContentView: View {

    @State var cantidad = ""
     @State private var numberOfPeople  = ""
    @State private var tippercentage  = 2
    let tipPercentages = [10,15,20,25,0]

    var totalPersonas: Double{
        //Funcion que calcula el total de personas
        //let conteopersonas = Double(numberOfPeople + 2  )
        let conteopersonas = Double(numberOfPeople) ?? 0
        let PropinaSeleccion = Double(tipPercentages[tippercentage])
        let MontoOrden = Double(cantidad) ?? 0

        let propinaValor = MontoOrden / 100 * PropinaSeleccion
        let Totalgeneral = MontoOrden + propinaValor
        let MontoPersonas = Totalgeneral / conteopersonas
        return MontoPersonas
    }

    var metoMontotalcuenta: Double {
        let PropinaSeleccion2 = Double(tipPercentages[tippercentage])
       let MontoOrden2 = Double(cantidad) ?? 0

        let propinavalor2 = MontoOrden2 / 100 * PropinaSeleccion2
        let TotalGeneralCuenta = MontoOrden2 + propinavalor2


        return TotalGeneralCuenta
    }



    var body: some View {

        NavigationView{
        Form {
            Section (header: Text("Monto de la cuenta"))  {
                 TextField("Ingresa Monto de la cuenta", text: $cantidad)
                    .keyboardType(.decimalPad)

              //  Picker("Numero de personas", selection: $numberOfPeople){
              //      ForEach(2 ..< 100) {
              //          Text("\($0) people")
              //      }
                //}

                Section (header: Text("Numero de Personas")){//seccion Personas
                    TextField("Ingresa El numero de personas", text:$numberOfPeople)
                    .keyboardType(.decimalPad)
                           }//seccion Personas

            }//Seccion1
            Section(header: Text("Cuanta propina quieres dejar")) {

            Picker("Tipo de porcentage", selection: $tippercentage){

                ForEach(0 ..< tipPercentages.count){
                    Text("\(self.tipPercentages[$0])%")
                }//ForEach

            }//Picker
            .pickerStyle(SegmentedPickerStyle())


            }//Seccion3
            //Seccion 2
            Section (header: Text("Cantidad por persona")) {

              Text("Cada persona pone:$\(totalPersonas)")
            } //Seccion 2

            Section{//seccion 4
                 Text("Monto total de la cuenta:$\(metoMontotalcuenta)")
            }//seccion 4






        } //Form
        .navigationBarTitle("Propinas SwiftUi")
        } //NavigationView
             .gesture(DragGesture().onChanged{_ in UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)})

    }


}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
4

1 回答 1

0

为您提供的快速解决方法:

    private var customBinding: Binding<Int> {
        Binding(get: {
            self.tippercentage
        }, set: {
            self.tippercentage = $0
            UIApplication.shared.windows.forEach { $0.endEditing(true )}
        })
    }

然后传递customBindingPicker.selection: customBinding

这个问题有更复杂的解决方案,我只认为这是一种快速的解决方法。

于 2020-05-25T03:02:51.073 回答