Your First Swift 4 & iOS 11 App - Part 14: | Ray Wenderlich

In this challenge, you'll come across your first bug - and you'll try and fix it.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3990-your-first-swift-4-ios-11-app/lessons/14

Would this be the better way to fixed the bug:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var slider: UISlider!
var currentValue: Int = 0
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    currentValue = getSliderValue(slider)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func showAlert() {
    
    let message = "The value of the slider is: \(currentValue)"
    let alert = UIAlertController(title: "Hello World", message: message, preferredStyle: .alert)
    
    let action = UIAlertAction(title: "OK", style: .default, handler: nil)
    
    alert.addAction(action)
    
    present(alert, animated: true, completion: nil)
}

@IBAction func sliderMoved(_ slider: UISlider) {
    
    currentValue = getSliderValue(slider)
}

func getSliderValue(_ slider: UISlider) -> Int {
    
    return lrintf(slider.value)
}

}

You’re exactly right! That way you don’t have to remember to initialize the slider to the same value you set in the Storyboard editor. I actually mention this later on in the course - you’re a bit ahead! :]

Thanks.

I’m not new to programming (but new to IOS/Mac programming). I was thinking of remaking this and pull the game logic out into its own game class.

This topic was automatically closed after 166 days. New replies are no longer allowed.