James Raitsev
James Raitsev

Reputation: 96541

Simple slider app, quick question

Everything looks right, and yet something is missing. Could you please take a look?

.m

@synthesize textLabel;
@synthesize slider;

- (IBAction)setLabel:(id)sender {
   int res = [slider value]; 
   NSLog(@"something changed %d", res);

[textLabel setText:[NSString stringWithFormat:@"%d",[slider value]]];
}

.h

@interface SlidaAppDelegate : NSObject {
    UILabel *textLabel;
    UISlider *slider;
    NSString *text;
}

- (IBAction)setLabel:(id)sender;

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UILabel *textLabel;
@property (nonatomic, retain) IBOutlet UISlider *slider;

Whenever slider moves, i see value update correctly, however simulator shows strange numbers enter image description here

Simulator shows

enter image description here enter image description here

Xib setup is as follows:

enter image description here

Please advise

*UPDATE <---------------------------------------- *

Problem went away when i changed

[textLabel setText:[NSString stringWithFormat:@"%d",[slider value]]];

to

[textLabel setText:[NSString stringWithFormat:@"%d",res]];

I don't fully understand why that it though, considering that

int res = [slider value]; 

If possible, please explain

Upvotes: 4

Views: 256

Answers (1)

Mark
Mark

Reputation: 408

My bet is that value returns a float which is in between 0 and 1. So when you assign it to an int, it gets rounded down to 0.

Make res a float.

EDIT

%d wants an int, you were giving it a float with [slider value]. So it must have interpreted that value in a strange way. See DOC

Upvotes: 4

Related Questions