Swiftui在表单上有多个选择器的构建问题

时间:2019-10-05 12:23:57

标签: swiftui

我正在学习SwiftUI(有经验的ios / swift开发人员),并且在添加额外的选择器控件时遇到了构建问题。请参见下面的代码。当我添加第三个选择器并尝试对其进行构建时,它只是挂起,并最终在另一个位置返回错误。删除任何选择器,然后构建代码,我可以运行它。不禁以为我在这里做些愚蠢的事情(如果我事先表示歉意)

import SwiftUI

struct CheckoutView: View {

    @EnvironmentObject var order: Order

    static let paymentTypes = ["Cash", "Credit Card", "iDine Points"]
    static let tipAmounts = [10, 15, 20, 25, 0]
    static let pickupTimes = ["Now", "Tonight", "Tomorrow"]

    @State private var paymentType = 0
    @State private var addLoyaltyDetails = false
    @State private var loyaltyNumber = ""
    @State private var testText = ""
    @State private var tipAmount = 1
    @State private var pickupTime = 0
    @State private var showingPaymentAlert = false

    var totalPrice: Double {
        let total = Double(order.total)
        let tipValue = total / 100 * Double(Self.tipAmounts[tipAmount])
        return total + tipValue
    }

    var body: some View {
        Form {
            Section {
                Picker("How do you want to pay?", selection: $paymentType) {
                    ForEach(0 ..< Self.paymentTypes.count) {
                        Text(Self.paymentTypes[$0])
                    }
                }
                Toggle(isOn: $addLoyaltyDetails.animation()) {
                    Text("Add iDine loyalty card")
                }
                if addLoyaltyDetails {
                    TextField("Enter your iDine ID", text: $loyaltyNumber)
                }
            }
            Section(header: Text("Add a tip?")) {
                Picker("Percentage:", selection: $tipAmount) {
                    ForEach(0 ..< Self.tipAmounts.count) {
                        Text("\(Self.tipAmounts[$0])%")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
            // get build error when adding this section...
            Section(header: Text("Pickup Time")) {
                Picker("Time:", selection: $pickupTime) {
                    ForEach(0 ..< Self.pickupTimes.count) {
                        Text(Self.pickupTimes[$0])
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
            Section(header: Text("TOTAL: $\(totalPrice, specifier: "%.2f")").font(.largeTitle)) {
                HStack {
                    Spacer()
                    Button("Confirm order") {
                        self.showingPaymentAlert.toggle()
                    }
                    .padding(8)
                    .font(.headline)
                    .background(RoundedRectangle(cornerRadius: 8.0).fill(Color.blue))
                    .foregroundColor(.white)
                }
            }
        }
        .navigationBarTitle(Text("Payment"), displayMode: .inline)
        .alert(isPresented: $showingPaymentAlert) {
            Alert(title: Text("Order confirmed"), message: Text("Your total was $\(totalPrice, specifier: "%.2f") – thank you!"), dismissButton: .default(Text("OK")))
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我也遇到表单选择器的问题,但是按如下所示更改循环时效果很好:-

ForEach(self.array.indices, id: \.self) { (index: Int)