views:

159

answers:

2

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

+6  A: 

Hi,

Try:

-(IBAction)sliderMoved:(id)sender {
    UISlider *slider = (UISlider *)sender;
    NSLog(@"SliderValue ... %d",(int)[slider value]);
}
Substance G
That does work thank you. As I state above I know using (id)sender is the convention but it would seem that statically typing the method to (UISlider *) would both be simpler and easier to follow, unless there is another reason to use (id)sender. This would seem to be a situation where convention is working against us, a convention for conventions sake?
fuzzygoat
@fuzzygoat - Don't forget to hit the checkmark.
TechZen
+1  A: 

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?

The Objective-C runtime relies heavily on naming conventions to find methods during runtime. You might get away with non-standard method names many time before it bites you on the backside. It's better to not chance it.

TechZen