簡體   English   中英

在 SwiftUI 中將選取器更改為文本字段

[英]Changing A Picker to a TextField in SwiftUI

我是一個自學 iOS 開發的新手(Paul Hudson 的 100DaysOfSwiftUI,項目 1),目前我遇到了一個我還沒有解決的問題。 最初,numberOfPeople 部分有一個帶有以下代碼的選擇器,我試圖將其更改為文本字段。 首先將其 @State 屬性 (@State private var numberOfPeople = 2) 從 2 更改為空字符串,並嘗試為人數創建另一個計算屬性。 當人數文本字段留空時,當我運行項目時,模擬器上每人數量部分的默認值不會顯示為 0,而是顯示為 NaN,如下面的屏幕截圖所示。 (這讓我想知道 numberOfPeople 是否通過如下 nil 合並成功轉換為 Integer)。 只有當我在人數文本字段中輸入值時,項目才能正常運行。 我不是 100% 確定我哪里出錯了,但是任何幫助都將不勝感激!

struct ContentView: View {
    @State private var checkAmount = ""
    @State private var numberOfPeople = ""
    @State private var tipPercentage = 2
    
    let tipPercentages = [10,15,20,25,0]
    let allPeople = 2...100

    var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople) ?? 0
        let tipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(checkAmount) ?? 0
        
        let tipValue = orderAmount / 100 * tipSelection
        let grandTotal = orderAmount + tipValue
        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }
    
    var grandTotal: Double {
        let tipSelection = Double(tipPercentages[tipPercentage])
        let orderAmount = Double(checkAmount) ?? 0
        
        let tipValue = orderAmount / 100 * tipSelection
        let totalAmount = orderAmount + tipValue
        
        return totalAmount
    }
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Amount", text: $checkAmount)
                        .keyboardType(.decimalPad)
                    
                    TextField("Number of people", text: $numberOfPeople)
                        .keyboardType(.numberPad)
                }
                
                Section(header: Text("How much tip do you want to leave?")) {
                    Picker("Tip percentage", selection: $tipPercentage) {
                        ForEach(0 ..< tipPercentages.count) {
                            Text("\(self.tipPercentages[$0])%")
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }
                
                Section (header: Text("Total Amount")){
                    Text("$\(grandTotal, specifier: "%.2f")")
                }
                
                Section (header: Text("Amount Per Person")){
                    Text("$\(totalPerPerson, specifier: "%.2f")")
                }
            }
            .navigationBarTitle("WeSplit")
        }
    }
}

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

(Original code for picker)
Picker("Number of people", selection: $numberOfPeople) {
        ForEach(2 ..< 100) {
            Text("\($0) people")

(Original code for people count)
let peopleCount = Double(numberOfPeople + 2)

項目模擬器

你看到的是試圖除以零的結果。 如果在使用以下方法進行除法之前, peopleCount0 ,則可以通過從totalPerPerson function 返回0來避免該計算:

var totalPerPerson: Double {
    let peopleCount = Double(numberOfPeople) ?? 0
    let tipSelection = Double(tipPercentages[tipPercentage])
    let orderAmount = Double(checkAmount) ?? 0
    
    guard peopleCount > 0 else {
        return 0
    }
    
    let tipValue = orderAmount / 100 * tipSelection
    let grandTotal = orderAmount + tipValue
    let amountPerPerson = grandTotal / peopleCount

    return amountPerPerson
}

請記住,使用新的TextField ,而不是選擇器,即使您指定.decimalPad.numberPad ,用戶仍然可以在這些字段中輸入非數字值,因此您可能想要輸入如果發生這種情況,則會出現錯誤(或者,在 SO 上搜索有關僅接受數字的字段的解決方案)。 解決方案中指定了此類 TextField 的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM