【发布时间】:2022-10-05 03:48:01
【问题描述】:
嗨,对不起,如果这是一个愚蠢的问题。
我一直在关注 HackingWithSwift SwiftUI 教程第 19 天(link)
但我面临一个问题。我尝试使用.focus() 修饰符和布尔值来处理按钮并在用户按下完成时隐藏我的键盘。每当我试图隐藏我的键盘时,我都有 2 个“完成”按钮,即使我只是在我的 UI 中添加了一个,即使我删除了那个按钮,它也不会显示任何“完成”按钮,我也无法隐藏我的键盘.
注意:我在 Preview 和 iPhone Simulator 上试过,但没有在物理设备上试过。
我还添加了屏幕截图和代码。
这是我的代码:
// ContentView.swift
// WeSplit
import SwiftUI
struct ContentView: View {
@State private var checkAmount = 0.0
@State private var numberOfPeople = 2
@State private var tipPercentage = 20
@FocusState private var amountIsFocused: Bool
let tipPercentages = [10, 15, 20, 25, 0]
//For Total cost + tip
var grandTotal: Double{
let tipSelection = Double(tipPercentage)
let tipValue = checkAmount / 100 * tipSelection
let grandTotal = checkAmount + tipValue
return grandTotal
}
//For individual share
var totalPerPerson: Double{
let peopleCount = Double(numberOfPeople + 2)
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var body: some View {
NavigationView {
Form{
Section{
TextField("Amount: ", value: $checkAmount, format: .currency(code: Locale.current.currencyCode ?? "USD"))
.keyboardType(.decimalPad)
.focused($amountIsFocused)
Picker("Number of people", selection: $numberOfPeople){
ForEach(2..<100){
Text("\($0) people")
}
}
}
Section{
Picker("Tip Percentage", selection: $tipPercentage){
ForEach(tipPercentages, id:\.self){
Text($0, format: .percent)
}
}.pickerStyle(.segmented)
}header: {
Text("How much tip do you want to leave?")
}
//Grand Total
Section{
Text(grandTotal, format:.currency(code: Locale.current.currencyCode ?? "USD"))
}header: {
Text("Total Cost + Tip")
}
//Showing each person's share
Section{
Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
}header: {
Text("Amount Per Person")
}.navigationTitle("WeSplit")
.toolbar(){
ToolbarItemGroup(placement: .keyboard){
Button("Done"){
amountIsFocused = false
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是屏幕截图:
【问题讨论】:
标签: ios swift iphone xcode swiftui