繁体   English   中英

选择器(SegmentedPickerStyle)中的选择效果不佳 - SwiftUI

[英]Selection in Picker (SegmentedPickerStyle) isn't working well - SwiftUI

我有风格SegmentedPickerStyle()的选择器。 现在我正在根据选择器值进行一些计算,并且它正在工作。 但不知何故,如果我点击它,我的选择器不会改变它的选择值。 要更改选择值,我需要滑动这些值。
另一个问题是,如果我需要计算TipAmount那么首先我需要滑动到选择器值,然后需要 select 该值。 我不知道为什么会这样!!

这是我的查看代码,

struct ContentView: View {

    @ObservedObject var tipViewModel = TipViewModel()

    var body: some View {

        VStack {

            TextField("Enter bill amount", text: $tipViewModel.billAmount)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            Picker(selection: $tipViewModel.tipPercentage, label: Text("Select tip %")) {
                ForEach(tipViewModel.tipChoices, id: \.self) { choice in
                    //Text("\(self.tipViewModel.tipChoices[choice])")
                    Text("\(choice)")
                }
            }.pickerStyle(SegmentedPickerStyle())
                .onTapGesture {
                    self.tipViewModel.calculateTip()
            }
            Text("Tip Percentage \(tipViewModel.tipPercentage)")
            Text(tipViewModel.tipAmount == nil ? "Tip Amount" : "\(tipViewModel.tipAmount!)")
        }.padding()
    }
}

这里是 TipViewModel 文件,

class TipViewModel: ObservableObject {

    @Published var billAmount: String = "" {
        didSet{
            calculateTip()
        }
    }
    @Published var tipPercentage: Int = 10
    @Published var tipAmount: Double = 0

    let tipChoices = [10, 15, 20]

    let didChange = PassthroughSubject<TipViewModel, Never>()

    func calculateTip() {

        guard let billAmount = Double(billAmount) else { return }
        self.tipAmount = billAmount * Double(tipPercentage) / 100
        self.didChange.send(self)
    }
}

任何帮助,将不胜感激!!

您有一个onTapGesture修改器,它正在吃选择器上的点击手势并尝试计算提示。

 .onTapGesture {
                self.tipViewModel.calculateTip()
        }

您可以将tipAmount 计算为计算属性:

struct ContentView: View {

    @ObservedObject var tipViewModel = TipViewModel()

    var body: some View {

        VStack {

            TextField("Enter bill amount", text: $tipViewModel.billAmount)
                .textFieldStyle(RoundedBorderTextFieldStyle())

            Picker(selection: $tipViewModel.tipPercentage, label: Text("Select tip %")) {
                ForEach(tipViewModel.tipChoices, id: \.self) { choice in
                    //Text("\(self.tipViewModel.tipChoices[choice])")
                    Text("\(choice)")
                }
            }.pickerStyle(SegmentedPickerStyle())

            Text("Tip Percentage \(tipViewModel.tipPercentage)")
            Text("\(tipViewModel.tipAmount)")
        }.padding()
    }
}

class TipViewModel: ObservableObject {

    @Published var billAmount: String = ""
    @Published var tipPercentage: Int = 10

    var tipAmount: Double {
        guard let billAmount = Double(billAmount) else { return 0 }
        let tipAmount = billAmount * Double(tipPercentage) / 100
        return tipAmount
    }

    let tipChoices = [10, 15, 20]

}

由于您已经将billAmounttipPercentage作为@Published属性,因此您无需担心向 PassthroughSubject 发送消息以告诉视图在计算属性更改时进行更新。 当小费金额或总金额发生变化时,将发送 willChange 消息。 这将导致视图刷新,这将调用tipAmount 计算属性,该属性将运行其闭包并将新值返回给视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM