Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何将条件修饰符链接到选定值?_Swift_Swiftui_Conditional Statements_Modifier - Fatal编程技术网

Swift 如何将条件修饰符链接到选定值?

Swift 如何将条件修饰符链接到选定值?,swift,swiftui,conditional-statements,modifier,Swift,Swiftui,Conditional Statements,Modifier,我目前正在使用Swift进行黑客攻击,100天的Swift,我有点被以下问题困扰 我正在尝试使用条件修改器根据用户选择的提示是否为0%来更改前景色,但几个小时后,如果提示选择为0%,我正在努力尝试将@State从false更改为true 我认为这可能是一个理解问题,因为我一直在尝试生成与视图不一致的错误的if语句,所以我想知道是否应该在这里使用视图修饰符/自定义视图,也许我只是不理解这是课程材料的一部分 我的代码如下: import SwiftUI struct ContentView: V

我目前正在使用Swift进行黑客攻击,100天的Swift,我有点被以下问题困扰

我正在尝试使用条件修改器根据用户选择的提示是否为0%来更改前景色,但几个小时后,如果提示选择为0%,我正在努力尝试将@State从false更改为true

我认为这可能是一个理解问题,因为我一直在尝试生成与视图不一致的错误的if语句,所以我想知道是否应该在这里使用视图修饰符/自定义视图,也许我只是不理解这是课程材料的一部分

我的代码如下:

import SwiftUI


struct ContentView: View {

@State private var restaurantBill = ""
@State private var diners = 2
@State private var selectedTip = 2

let tipAmount = [5, 10, 15, 20, 25, 0]

@State private var noTipSelected = false // <-- no tip selected state is equal to false

// computed properties
var totalPerPerson: Double {
    let peopleToCount = Double(diners + 2)
    let tipSelection = Double(tipAmount[selectedTip])
    let orderAmount = Double(restaurantBill) ?? 0
    
    let tipValue = orderAmount / 100 * tipSelection
    let grandTotal = tipValue + orderAmount
    let costPerPerson = grandTotal / peopleToCount
    
    return costPerPerson
}

var totalOverall: Double {
    let tipSelection = Double(tipAmount[selectedTip])
    let orderAmount = Double(restaurantBill) ?? 0
    
    let tipValue = orderAmount / 100 * tipSelection
    let grandTotal = tipValue + orderAmount
    
    return grandTotal
}



    
var body: some View {
    NavigationView {
        Form {
            Section {
                TextField("£ total bill amount", text: $restaurantBill)
                    .keyboardType(.numberPad)
            }
            
            Picker("Table for: ", selection: $diners){
                ForEach(2..<100){
                    Text("\($0) diners")
                }
            }
            
            Picker("Select Tip: ", selection: $selectedTip){
                ForEach(0..<tipAmount.count){
                    Text("\(self.tipAmount[$0])%")
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            
            
            Section {
                Text("Total bill is: £\(totalOverall, specifier: "%.2f")")
                    .foregroundColor(noTipSelected ? .green : .red) //<---- my conditional modifier
            }
            
            
            Section {
                Text("Each diner pays: £\(totalPerPerson, specifier: "%.2f")")
                    
                    
            }
        }
        .navigationBarTitle("WeSplit v2.0")
    }
}

任何想法都值得赞赏。

您在哪里尝试更改
而不是选择
?你也可以选择使用
.foregroundColor(tipAmount==5?.green:.red)
。我同意,你不需要单独的@State变量,你可以使用
selectedTip
来实现这一点(我认为这是George在评论中真正的意思,而不是tipAmount)@JoakimDanielson是的,你是对的,我的意思是
selectedTip
——或者更明显的内涵,
tipAmount[selectedTip]==0
。如果将来调整
tipAmount
也会更好。多亏了大家,我做了这个更改
(tipAmount[selectedTip]==0?。红色:。绿色)
,效果很好。看起来我还犯了一个错误,假设@State属性是必需的,而它不是必需的!这就解释了为什么我一直在兜圈子。
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}