Using The Value Of A Slider As An Index In iPhone

The UISlider control’s value is a floating point number between its minimum and maximum values, inclusive. So how can we use the value of a slider to index into an array? Read on to find out!

Start Xcode, choose “Create a new Xcode project” and select the Single View Application template. Name the application “SliderList,” and choose options as shown here:

Click Next, choose a location to save the project, and click Create.

In ViewController.xib, drag a slider and a label to the view as shown:

Change the text of the label to read “January”, and min and max values of the slider to 0 and 11. Make sure the current value of the slider is set to 0.

Open ViewController.h and make these changes:

 

#import <uikit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, weak) IBOutlet UILabel *month;

- (IBAction)sliderValueChanged:(UISlider *)sender;

@end

 

 

The month outlet should be wired to the UILabel control and the sliderValueChanged method should be wired to the slider’s ValueChanged event in Interface Builder.

Now open ViewController.m and make these changes:

 

#import "ViewController.h"

NSArray *monthArray;

@interface ViewController ()

@end

@implementation ViewController

@synthesize month;

- (IBAction)sliderValueChanged:(UISlider *)sender
{
self.month.text = [monthArray objectAtIndex:sender.value];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
monthArray = [NSArray arrayWithObjects:
@"January", @"February", @"March",
@"April", @"May", @"June",
@"July", @"August", @"September",
@"October", @"November", @"December", nil];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

 

 

Note that we’ve made a private variable called monthArray at the top of the file. This NSArray doesn’t need to be seen by any class but this one, so this is the proper way to do this. We instantiate and initialize this array in viewDidLoad with the months of the year as NSStrings.

In sliderValueChanged: it is a simple matter of indexing into the monthArray using the slider’s value. But array indexes are int, and the sender’s value is float! How does this work?

The answer is C’s type conversion logic. If the value of an expression doesn’t match the value the receiver expects, the compiler will make an attempt to cast the expression result to the expected value of the receiver. Here, the floating point value of the slider is being implicitly typed to int!

Run the app, and change the value of the slider to see each month in the array:

Leave a Comment:

3 comments
John Avatar says October 21, 2012

I tried your code using Xcode 3.2.6 and Xcode 4.2. I haven’t tried it with the latest Xcode. It looks like you used Xcode 4, but I don’t know which version? Please tell us, in the future, which Xcode you use…

BUT… your code doesn’t work?

Can you please provide the completed project for download?

For some reason, you seem to think Xcode will return;
@interface ViewController : UIViewController

But it doesn’t, it returns;
@interface SliderListViewController : UIViewController {

That’s the first thing that’s confusing and causes you code to not work. I changed your’s to what Xcode returns, then made other changes to your code as follows, to try to get it to work.
****************************************

I made some changes to your project for Xcode 3.2.6 and got it to almost work, but not quite. It’s weird though, it still doesn’t quite work, at one point it almost worked, but then it like… stopped?

Here’s what I did… Please tell me what’s wrong?
****************************************

for SliderListViewController.h
****************************************

#import

@interface SliderListViewController : UIViewController {

UILabel *month;
IBOutlet UISlider *sliderValue1;

}

@property (nonatomic, retain) IBOutlet UILabel *month;

//- (IBAction)sliderValueChanged:(UISlider *)sender;

-(IBAction) sliderValueChanged:(id) sender;

@end

****************************************
FOR SliderListViewController.m I added and made changes to yours as follows;
****************************************

NSArray *monthArray;

//@interface ViewController ()

@synthesize month;

– (IBAction)sliderValueChanged:(UISlider *)sender
{
self.month.text = [monthArray objectAtIndex:sender.value];
}

– (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
monthArray = [NSArray arrayWithObjects:
@”January”, @”February”, @”March”,
@”April”, @”May”, @”June”,
@”July”, @”August”, @”September”,
@”October”, @”November”, @”December”, nil];
}

Reply
John Avatar says October 22, 2012

Your following code causes the APP to crash in the Simulator?
self.month.text = [monthArray objectAtIndex:sender.value];

If I replace that with the following code, it loads “December” and works, but not to select from the array;
self.month.text = @”December”;

So… what’s wrong with this code?
self.month.text = [monthArray objectAtIndex:sender.value];

Thank you,
John Avatar
JohnAvatar.com

Reply
John Avatar says October 22, 2012

Ok… can I delete the first two comments?

I tried it again.. using Xcode 4.2, exactly as you said and it works fine!

You actually do have an “XYZ” in the “Class Prefix” window, which can cause problems. If you leave that blank, it all works fine!

So… Thank You, great tutorial… now I’ve got something to work with.

John Avatar
JohnAvatar.com

Reply
Add Your Reply