Home > Net >  How to reference a value of a constant from a different view Swift
How to reference a value of a constant from a different view Swift

Time:09-02

I would like to reference a value of a constant from a different view. I looked online how to do so but most of the search results are outdated or don't work.

I would like to reference the value of oneRepMax in a struct ProgressiveOverload.

I want to import oneRepMax from here:

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)
    }
}

To here:

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()
    }
}

CodePudding user response:

Just pass it on as an argument:

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

and call it:

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