Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

StevenHu

macrumors member
Original poster
Sep 3, 2009
80
0
Southern CA
I do not want my iPhone slider to return simple numbers like 1, 2, 3, etc. to the text label, but 15mm, 15.5mm, 16mm, etc., from 15mm to 45mm in increments of .5. So I thought I would put all the acceptable numbers in an array and link the slider output with the corresponding object in the array. (Slider returning "0" will link to the first element of the array, etc.)

Here is how I've tried to link the output to the array, and the warning it generated (line 5, for line 4):

Code:
-(void)sliderAction:(id)sender{
NSArray *array = [NSArray arrayWithObjects: @"15", @"15.5", ..., @"45", nil];
NSString *labelValue; // This var will pass the value to the text label
labelValue = [array objectAtIndex:slider.value]; // Slider will pass a number from 0-59
// warning: passing argument 1 of 'objectAtIndex' makes integer from pointer without a cast
label.text = [NSString stringWithFormat:@"%02.0fmm", labelValue]; // The array number and "mm" will be sent to the text label
[labelValue release]; // release the memory
}

How do I change the type from a number to a string (I think that's what will take away the warning)? I've been coding in Obj-C for the last three months, and came from a PHP background.

Thanks!
Steve
 
if slider is an UISlider, it will return a float value between 0 and 1. And from there you should interpret it into a value. Using an array in that manner may not be the best way either, it may be easier to do something like this
--psuedocode--
int start = 10;
int end = 20;
double step =.5;
int amountOfSteps = (end-start) *(1/step);
int place = (int) slider.value*amountOfSteps;
double printNumber = start+place/step;
and the reason because of your error is slider.value needs to be casted into an int with "(int)".
 
I put (int) in front of slider.value, as in
(int)slider.value

and it returned the same error. I put (int) in front of value, as in
slider.(int)value

and got the same error. Did I misunderstand you?

As for why I am using an array: I have several other sliders on the view, and their numbers to be echoed to the label are irregularly spaced, custom values, unlike the above. Once I get this array method working, then I'll be in good shape to make all the other sliders work.

The %02.0f returns 2-place integers with no decimal spaces to the right, enabling me to use those integers to point to the elements in the array. I thought it was a clever idea!

Please clarify what I'm supposed to do with the int. I know in C I can put:
int slider;
and it will be an int, but that didn't work in this case.

Thanks for the help!
Steve
 
if slider is an UISlider, it will return a float value between 0 and 1.
Definitely a float but the value depends on what the minimumValue and maximumValue for the UISlider are set to (granted, the defaults are 0.0 and 1.0).

I tried:
Code:
labelValue = [array objectAtIndex:(int)slider.value];
and got no warning.

And since you've already converted the slider value to a string from the array, you probably can just use this line for your label text setting:
Code:
label.text = [NSString stringWithFormat:@"%@mm", labelValue];

P.S. Don't [labelValue release] at the end, since you never allocated it.
 
@steven, what is the declaration of slider?

There's nothing wrong with this line if slider is a UISlider*. The warning says you're trying to convert a pointer into an int. But UISlider value returns a float, not a pointer.

Code:
labelValue = [array objectAtIndex:slider.value];

Cleverness aside, this line can never work

Code:
label.text = [NSString stringWithFormat:@"%02.0fmm", labelValue];

You're trying to print an NSString as if it were a float.
 
In order for the slider output to match up with the array elements, the min = 0.0 (for the first element in the array) and the max = 59.0.

OK, here's what I have now:

Code:
-(void)sliderAction:(id)sender{
NSArray *array = [[NSArray alloc]arrayWithObjects: @"15", @"15.5", ..., @"45", nil];
NSString *labelValue; // This var will pass the value to the text label
labelValue = [array objectAtIndex:(int)slider.value]; // Slider will pass a number from 0-59 (the min and max values set)
label.text = [NSString stringWithFormat:@"%@mm", labelValue]; // The array number and "mm" will be sent to the text label
}

There is no error until I try to move the slider in the simulator, then I get a TERMINATING DUE TO UNCAUGHT EXCEPTION. The debugger console says, "... reason: [__NSPlaceholderArray arrayWithObjects:]: unrecognized selector sent to instance 0xd05d20' ..."

What does that mean? Perhaps it means it's not pointing to an element in the array?

@PhoneyDeveloper, the following works without errors if I wanted to output just the slider's value apart from using the array. It passes 0-59 if the min is 0.0 and max is 59.0. I got that working before working on the array:

Code:
-(void)sliderAction:(id)sender{
label.text = [NSString stringWithFormat:@"%02.0fmm", slider.value];
}

When I tried this...
Code:
-(void)sliderAction:(id)sender{
label.text = [NSString stringWithFormat:@"%@mm", labelValue];
}

... the slider label returns "(null)mm" then crashes with "objc_msgSend."

Thanks!
Steve
 
Code:
NSArray *array = [[NSArray alloc]arrayWithObjects: @"15", @"15.5", ..., @"45", nil];
I think you want:
Code:
NSArray *array = [NSArray arrayWithObjects: @"15", @"15.5", ..., @"45", nil];
Also, you need to make sure that array has 60 elements in it. We can't be sure because we don't know what you actually have where the "..." is.

When I tried this...
Code:
-(void)sliderAction:(id)sender{
label.text = [NSString stringWithFormat:@"%@mm", labelValue];
}

... the slider label returns "(null)mm" then crashes with "objc_msgSend."
Well, you haven't even defined labelValue there yet so of course you're gonna have issues.
 
I double-checked the number of elements in the array. I was off; I corrected it.

I took out the alloc.

It works!

THANK YOU!

Now the next step is to add a UIScrollView so I can scroll the view (I'm starting with Apple's UICatalog app and this "segment" page does not scroll.)

Thanks again,
Steve
:)
 
THANK YOU!

Now the next step is to add a UIScrollView so I can scroll the view (I'm starting with Apple's UICatalog app and this "segment" page does not scroll.)
You're welcome.

Good luck with the UIScrollView! They can be a real pain sometimes (although probably not as painful as mixing UITTabBarController and UINavigationController). :D
 
Tell me about it! I've been working on it for a week and still have gotten nowhere. The slider issue was an interlude before jumping back to the scrolling issue.

Thanks,
Steve
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.