Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adjusting label to slider value swift

I have a slider and label in my swift project. On the storyboard, I control dragged my slider onto my controller class for that page and created an outlet and also an action. I control dragged another label as an outlet. I am trying to update the label based on the slider's value. I don't know where i am going wrong.

Code:

@IBOutlet weak var slider: UISlider!

@IBOutlet weak var sliderVal: UILabel!

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    println("Slider changing to \(currentValue) ?")
    sliderVal.text = "\(currentValue) Km"
} 

I can see in the log the sliderValueChanged funciton is being called and the log is printing the value but the label's text is not updating. What am I doing wrong?

Update:

I just put a slider object and label on my login screen and used the same methodology and code to change the label text and it worked but it will not work inside my tab bar controller. Does this shed any light on what the issue may be?

like image 329
user2363025 Avatar asked Jun 04 '15 09:06

user2363025


2 Answers

Update the slider value in Main queue

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    println("Slider changing to \(currentValue) ?")
    dispatch_async(dispatch_get_main_queue(){
        sliderVal.text = "\(currentValue) Km"
    })
}
like image 131
MMT Avatar answered Nov 19 '22 19:11

MMT


class ViewController: UIViewController {

@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var slideroutlet: UISlider!
@IBOutlet weak var lblValue: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func slider(_ sender: Any) {

    lblValue.text = String(slideroutlet.value)
    imgView.alpha = CGFloat(slideroutlet.value)
}
like image 3
Hiren Avatar answered Nov 19 '22 19:11

Hiren