0

I am working on a BMI calculator as part of a wider app.

The idea is that the user calculates the BMI then goes to another screen where they find the BMI chart to compare their result.

The idea I have is that the calculated BMI result on the BMI screen is also displayed on the chart screen as a reminder of the score.

I have been attempting to transfer the calculated score (displayed on the calculation screen as a UILabel) across a segue and display it in another UILabel at the top of the screen.

I have been attempting to use the "prepare(for segue)" and "performSegue" commands but can't seem to get the data to transfer across.

I am very new to coding and no doubt I am doing something stupid, but if anyone can help me figure this out, I would be really grateful.

I have linked the screen via the "Active Segue" and have identified the Segue as "bmiSeg. The calculation view controller is named "BMICalc" and the chart view controller named "BMI_Result"

The UILabel on BMICalc is labelled bmiScoreLabel with a var called bmiResult used to collect the score. The UILabel on BMI_Result is labelled bmiResultLabel with a var called bmiScore.

The goBMIButton takes the user to the chart screen.

Hope all that makes sense!

My current coding for the transfer is as follows:

@IBAction func goBMIButton(_ sender: UIButton) {
        let generator = UINotificationFeedbackGenerator()
        generator.notificationOccurred(.success)
        
        performSegue(withIdentifier: "bmiSeg", sender: self)
        
        
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "bmiSeg" {
            let destinationVC = segue.destination as? BMI_Result
            destinationVC?.bmiScore = Int(bmiResult)
        }
    }

On the second screen the target is simply the UILabel:

I have asked the data transfer to go into var bmiScore and then from there into the UILabel as follows:

bmiResultLabel.text = " (bmiScore)"

Hopefully that description of my thinking isn't too confusing and my apologies if it is!

Thanks everyone!!

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Dan Meese
  • 3
  • 1
  • 1
    Is the problem just with transferring the value to the second view? Either way `bmiResultLabel.text = " (bmiScore)"` is missing a \, it should be `bmiResultLabel.text = "\(bmiScore)"` – Joakim Danielson Dec 20 '22 at 11:24

1 Answers1

0

On the second screen

var bmiScore:Int? {
  didSet {
     bmiResultLabel.text = "\(bmiScore)"
  }
}

DidSet is here to set the title of label whenever the bmiscore interrupted or get new value

Zeeshan Ahmad II
  • 1,047
  • 1
  • 5
  • 9