好吧,我是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()
}
}
答案 0 :(得分:0)
为您提供的快速解决方法:
private var customBinding: Binding<Int> {
Binding(get: {
self.tippercentage
}, set: {
self.tippercentage = $0
UIApplication.shared.windows.forEach { $0.endEditing(true )}
})
}
然后像customBinding
一样将Picker
传递到selection: customBinding
。
对此问题有更复杂的解决方案,我只认为这是一种快速的解决方法。