skip to Main Content

I successfully created a slider for user height in storyboard and the corresponding code to assign a value in the slider to each level of height but as you can see below, I had to manually copy & paste each new height variable to get it to work. I was curious how an expert would simplify this code? thanks!

*note – I removed the code for height 5’2 to 6’4 to not take up so much space.

”’

@IBOutlet weak var yourHeightEquals: UILabel!

@IBOutlet weak var heightSliderOutlet: UISlider!

@IBAction func heightSliderAction(_ sender: UISlider) {
    
    heightSliderOutlet.value = roundf(heightSliderOutlet.value)
    
    let yourHeightText: String = "Your Height: "
    
    if heightSliderOutlet.value == 0 {
        
        let yourHeightString = "Choose Your Height"
        
        yourHeightEquals.text = yourHeightString
    }
    
    else if heightSliderOutlet.value == 1 {
        
        let yourHeightString = "<5'0"
        
        yourHeightEquals.text = yourHeightText + yourHeightString
    }
    
    else if heightSliderOutlet.value == 2 {
        
        let yourHeightString = "5'0"
        
        yourHeightEquals.text = yourHeightText + yourHeightString
    }
    
    else if heightSliderOutlet.value == 3 {
        
        let yourHeightString = "5'1"
        
        yourHeightEquals.text = yourHeightText + yourHeightString
    }
   

…….

    else if heightSliderOutlet.value == 19 {
        
        let yourHeightString = "6'5"
        
        yourHeightEquals.text = yourHeightText + yourHeightString
    }
    
    else if heightSliderOutlet.value == 20 {
        
        let yourHeightString = ">6'5"
        
        yourHeightEquals.text = yourHeightText + yourHeightString
    }
    
}

”’

2

Answers


  1. Chosen as BEST ANSWER

    Update - the switch code above only worked for a metric system - I had to modify it a bit for feet & inches, still much cleaner than my original code!

    @IBOutlet weak var yourHeightEquals: UILabel!
    
    @IBOutlet weak var heightSliderOutlet: UISlider!
    
    @IBAction func heightSliderAction(_ sender: UISlider) {
        
        let value = roundf(heightSliderOutlet.value)
        
        switch value {
        case 0:
            yourHeightEquals.text = "Choose Your Height"
        case 1:
            yourHeightEquals.text = "Your Height: <5'0"
        case 2...11:
            let height = 5.0 + ((value - 2) * 0.1)
            let heightstring = String(height)
            let heightfoot = heightstring.dropLast(2)
            let heightinch = heightstring.dropFirst(2)
            let heightfootinch = heightfoot + "'" + heightinch
            yourHeightEquals.text = "Your Height: (heightfootinch)"
        case 12:
            yourHeightEquals.text = "Your Height: 5'10"
        case 13:
            yourHeightEquals.text = "Your Height: 5'11"
        case 14...19:
            let height = 6.0 + ((value - 14) * 0.1)
            let heightstring = String(height)
            let heightfoot = heightstring.dropLast(2)
            let heightinch = heightstring.dropFirst(2)
            let heightfootinch = heightfoot + "'" + heightinch
            yourHeightEquals.text = "Your Height: (heightfootinch)"
        default:
            yourHeightEquals.text = "Your Height: >6'5"
        }
        
    }
    

  2. You can use a switch statement like this.

    @IBOutlet weak var heightSliderOutlet: UISlider!
    @IBOutlet weak var yourHeightEquals: UILabel!
    @IBAction func heightSliderAction(_ sender: UISlider) {
        let value = roundf(heightSliderOutlet.value)
        switch value {
        case 0:
            yourHeightEquals.text = "Choose Your Height"
        case 1:
            yourHeightEquals.text = "Your Height : <5'0"
        case 2...19:
            let height = 5.0 + ((value - 2) * 0.1)
            yourHeightEquals.text = "Your Height : (height)"
        default:
            yourHeightEquals.text = "Your Height: >6'5"
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search