繁体   English   中英

如何从不同的角度引用常量的值 Swift

[英]How to reference a value of a constant from a different view Swift

我想从不同的角度引用一个常量的值。 我在网上查看了如何做到这一点,但大多数搜索结果都已过时或不起作用。

我想在struct ProgressiveOverload中引用oneRepMax的值。

我想从这里导入oneRepMax

import SwiftUI

struct OneRepMaxCalculator: View {
    
    @State private var reps = 1.0
    @State private var weight = 0.0
    
    var body: some View {
        NavigationView{
            Form{
                Section(header: Text("Weight")) {
                    TextField("Weight: \(weight.formatted())kg", value: $weight, format: .number)
                }
                Section(header: Text("Reps")) {
                    Stepper("Reps : \(reps.formatted())", value: $reps, in: 1...10)
                }
                
            //THIS CONSTANT
            let oneRepMax = reps == 1.0 ? weight : weight*(1+(0.0333*reps))-2
                
                
                if oneRepMax > 0 {
                    Text("Your 1RM is: \(oneRepMax.formatted())kg")
                        .font(.headline)
                }

                if weight > 0 {
                Section(header: Text("2RM-10RM")) {
                    Menu("View All From 2RM-10RM") {
                        Text(String(format: "Your 2RM is %.1fkg", oneRepMax/(1+(0.0333*2))+2, oneRepMax/(1+(0.0333*2))+2))
                                .font(.footnote)
                        Text(String(format: "Your 3RM is %.1fkg", oneRepMax/(1+(0.0333*3))+2, oneRepMax/(1+(0.0333*3))+2))
                                .font(.footnote)
                        Text(String(format: "Your 4RM is %.1fkg", oneRepMax/(1+(0.0333*4))+2, oneRepMax/(1+(0.0333*4))+2))
                                .font(.footnote)
                        Text(String(format: "Your 5RM is %.1fkg", oneRepMax/(1+(0.0333*5))+2, oneRepMax/(1+(0.0333*5))+2))
                                .font(.footnote)
                        Text(String(format: "Your 6RM is %.1fkg", oneRepMax/(1+(0.0333*6))+2, oneRepMax/(1+(0.0333*6))+2))
                                .font(.footnote)
                        Text(String(format: "Your 7RM is %.1fkg", oneRepMax/(1+(0.0333*7))+2, oneRepMax/(1+(0.0333*7))+2))
                                .font(.footnote)
                        Text(String(format: "Your 8RM is %.1fkg", oneRepMax/(1+(0.0333*8))+2, oneRepMax/(1+(0.0333*8))+2))
                                .font(.footnote)
                        Text(String(format: "Your 9RM is %.1fkg", oneRepMax/(1+(0.0333*9))+2, oneRepMax/(1+(0.0333*9))+2))
                                .font(.footnote)
                        Text(String(format: "Your 10RM is %.1fkg", oneRepMax/(1+(0.0333*10))+2, oneRepMax/(1+(0.0333*10))+2))
                                .font(.footnote)
                        }
                    }
                }
                
                if oneRepMax > 0 {
                    Section(header: Text("Progressive Overload")){
                    NavigationLink(destination: ProgressiveOverload()) {
                        Text("Apply Progressive Overload")
                        }
                    }
                }
                
                if weight == 0 && reps > 1 {
                Section(header: Text("Warning")){
                    Text("Add Weight First")
                        .font(.callout)
                    }.foregroundColor(Color.red)
                        
                }
                
            }.navigationBarTitle(Text("One Rep Max Calculator"))
        }
    }
}

struct OneRepMaxCalculator_Previews: PreviewProvider {
    static var previews: some View {
        OneRepMaxCalculator()
            .preferredColorScheme(.dark)
    }
}

到这里:

import SwiftUI
    
struct ProgressiveOverload: View {
    
    var body: some View {
        NavigationView{
            Form{
                Text("Your 1RM is \(oneRepMax)")
            }
        }
    }
}

struct ProgressiveOverload_Previews: PreviewProvider {
    static var previews: some View {
        ProgressiveOverload()
    }
}

只需将其作为参数传递:

struct ProgressiveOverload: View {
    let oneRepMax: Double
    var body: some View {
        NavigationView{
            Form{
                Text("Your 1RM is \(oneRepMax)")
            }
        }
    }
}

并称之为:

NavigationLink(destination: ProgressiveOverload(oneRepMax: oneRepMax)) {
    Text("Apply Progressive Overload")
    }
}

暂无
暂无

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

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