laarsk
laarsk

Reputation: 872

Objective C / iPhone - how to bind UISlider to code

I'm just starting with Objective C, but I'm already running into a problem: When I did this in the past, I had no problem with it: I made a new UISlider, clicked it, CTRL dragged a blue line to the code, and somehow I was then able to read it's values.

However, I am now running into the problem that I can't draw those lines anymore. By messing around I managed to get buttons to work, but the slider just won't work. my Mainwindow.xib has got the iSaturateViewController class, iSaturateViewController.xib has got the whole interface in it.

How can I get the UISlider to work without being able to draw that line?

How I have it now: (in iSaturateViewController.h)

#import <UIKit/UIKit.h>

@interface iSaturateViewController : UIViewController {
    UISlider * satSlider;
    UILabel * satLabel;
}

@property (nonatomic, retain) IBOutlet UISlider * satSlider;
@property (nonatomic, retain) UILabel * satLabel;

-(IBAction) changeSaturation:(id) sender;

@end

(in iSaturateViewController.m):

@synthesize satLabel,satSlider;

-(IBAction) changeSaturation:(id)sender {
    satLabel.text = [[NSString alloc] initWithFormat:@"Saturation:  %d ", (Float32)satSlider.value];
     NSLog(@"%f", satSlider.value);
}

Ath the moment the NSLog shows: 0.00000000

Also, I've set the Label in the Identity properties to: satSlider (slider) and satLabel (label).

Just to clarify my goal:

I want the slider's (satSlider) value to show in the UILabel (satLabel) via the function changeSaturation.

How can I do this?

Upvotes: 2

Views: 1859

Answers (3)

Rafał Sroka
Rafał Sroka

Reputation: 40030

Add the method declaration:

-(IBAction) changeSaturation:(id)sender;

to the header file iSaturateViewController.h

Then, assign the Value Changed action of your UISlider to your changeSaturation method. Right click on the slider and drag the Value Changed to the File's Owner and connect them. It should work.

You can also try this:

-(IBAction)changeSaturation:(id)sender {

     UISlider *slider = (UISlider *)sender;
     satLabel.text = [NSString stringWithFormat:@"Saturation: %f", slider.value];
     NSLog(@"Saturation: %f", slider.value);
}

Regarding UILabel - add IBOutlet and connect it in the Interface Builder. The interface should be like that:

#import <UIKit/UIKit.h>

@interface iSaturateViewController : UIViewController {
    IBOutlet UISlider * satSlider;
    IBOutlet UILabel * satLabel;
}

@property (nonatomic, retain) IBOutlet UISlider * satSlider;
@property (nonatomic, retain) IBOutlet UILabel * satLabel;

-(IBAction) changeSaturation:(id) sender;

@end

Let me know if it works.

Upvotes: 6

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

Looks like the key thing you’re missing is that the satSlider outlet isn’t hooked up, so its value is nil. You connected the slider’s action to the -changeSaturation: method, but it sounds like you didn’t also connect the satSlider property to the slider itself. Sending a message (like value in this case) to nil doesn’t do anything, and returns 0, so 0 is the value you’re getting out of it. Hook up your outlet by Ctrl-dragging from the property in the code to the slider in your UI and it should work.

Upvotes: 1

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

You need to edit your header and add the method declaration. Then in IB, associate the first responder for value changed to the UISlider. Also slider.value, you need to read article: Setting interval value for UISlider

Upvotes: 0

Related Questions