Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value from UISlider method?

Can anyone point me in the right direction regarding sliders, the version below was my first attempt (the range is 0.0 - 100.0) The results I get are not right.

// Version 1.0
-(IBAction)sliderMoved:(id)sender {
    NSLog(@"SliderValue ... %d",(int)[sender value]);
}

// OUTPUT:
// [1845:207] SliderMoved ... -1.991753
// [1845:207] SliderMoved ... 0.000000
// [1845:207] SliderMoved ... 0.000000
// [1845:207] SliderMoved ... 32768.000435

With the version below I get the values I expect, what am I missing in version_001?

// Version 2.0
-(IBAction)sliderMoved:(UISlider *)sender {
    NSLog(@"SliderValue ... %d",(int)[sender value]);
}

// OUTPUT:
// [1914:207] SliderMoved ... 1
// [1914:207] SliderMoved ... 2
// [1914:207] SliderMoved ... 3
// [1914:207] SliderMoved ... 4

EDIT_001:

Adding UISlider *slider = (UISlider *)sender; as Substance G points out does indeed work. I understand that using sender is the convention, but it seems to me in this case that not only is the convention less clear its also more complicated and more code. It would seem sensible IMO to statically type the method to (UISlider *) as in version 2.0. Is there a situation I am overlooking in the slider IBAction method where using (UISlider *) would not work?

cheers Gary

like image 315
fuzzygoat Avatar asked Jan 22 '26 04:01

fuzzygoat


1 Answers

Try:

-(IBAction)sliderMoved:(id)sender {
    UISlider *slider = (UISlider *)sender;
    NSLog(@"SliderValue ... %d",(int)[slider value]);
}
like image 50
SubG Avatar answered Jan 24 '26 17:01

SubG