Segmented control

I am adding a segmented control that can switch from male to female. It is for a nutrition app, so it has a range for healthy amounts of sugar, which are different between males and females. It does a calculation that results in a value of 1, 2, or 3 (sugar value) there is a text field where the amount of sugar consumed is entered and a label that shows 1, 2, or 3. It works for female, but the sugar value for male is always the same as female. I think I see the problem, but don’t know how to fix it. I’m very new to this, so please be patient.

@IBOutlet weak var controller: UISegmentedControl!

@IBOutlet weak var sugarField: UITextField!
@IBOutlet weak var label: UILabel!

var calorieRatio = 1.0
var sugarValue = 1.0
var sugarValueM = 1.0

@IBAction func changeLb(_ sender: Any)

{if controller.selectedSegmentIndex == 0 {        let sugarFieldConv :Double = Double(sugarField.text!)!
                          
                            
                if sugarFieldConv >= 16.8 * calorieRatio { sugarValue = 1.0 }
                         
                        
                    if sugarFieldConv <= 8.3 * calorieRatio { sugarValue = 3.0 }
                            
                      if sugarFieldConv > 8.3 * calorieRatio && sugarFieldConv < 16.8 * calorieRatio { sugarValue = 2.0 }

    else if controller.selectedSegmentIndex == 1 {
        
                        if sugarFieldConv >= 25 * calorieRatio { sugarValueM = 1.0 }
                                                 
                            if sugarFieldConv <= 12.5 * calorieRatio { sugarValueM = 3.0 }
                                                    
                                  if sugarFieldConv > 12.5 * calorieRatio && sugarFieldConv < 25 * calorieRatio { sugarValueM = 2.0 }
                  }
   if controller.selectedSegmentIndex == 0 { label.text = "\(sugarValue)" }
          if controller.selectedSegmentIndex == 1 { label.text = "\(sugarValueM)"}
}

}

}

Hi @Lea-D

Welcome to the community!

If female segment 0?

I reformatted your code so I could follow it easier for my dyslexic brain.

Does the constant belong in segment 0 or is it there for both as I have placed it.

 @IBAction func changeLb(_ sender: Any) {
        
        //Does this go here or in Segment 0
        //if sugerFieldConv is nil, you will crash
        let sugarFieldConv :Double = Double(sugarField.text!)!
        
        if controller.selectedSegmentIndex == 0 {
            
            if sugarFieldConv >= 16.8 * calorieRatio {
                sugarValue = 1.0
            }
            
            if sugarFieldConv <= 8.3 * calorieRatio {
                sugarValue = 3.0
            }
            
            if sugarFieldConv > 8.3 * calorieRatio && sugarFieldConv < 16.8 * calorieRatio { sugarValue = 2.0
            }
        //You were missing the else closing bracket
        } else if controller.selectedSegmentIndex == 1 {
            
            if sugarFieldConv >= 25 * calorieRatio { sugarValueM = 1.0
                
            }
            
            if sugarFieldConv <= 12.5 * calorieRatio { sugarValueM = 3.0 }
            
            if sugarFieldConv > 12.5 * calorieRatio && sugarFieldConv < 25 * calorieRatio { sugarValueM = 2.0
            }
        }
        if controller.selectedSegmentIndex == 0 {
            label.text = "\(sugarValue)"
            
        }
        if controller.selectedSegmentIndex == 1 {
            label.text = "\(sugarValueM)"
            
        }
    }
---

Blessings,
—Mark

Thanks for your help Mark,
Yes, index 0 is female
and I honestly don’t know how to properly format my code, so thanks for making those changes.

I made the changes you suggested and now when I test it, instead of index 1 (male) being the same as index 0, it is always the value I initialized it to. I know the else if statement will only execute if the if statements are false, so it will never execute because one of the index 0 if statements is always true . Is there another way I can write it where the index 1 (male) statements will run?

thanks. it’s working now.