3
votes

I follow the code of user2207904

on viewDidLoad I create 20 sliders and 20 labels

then my 20 labels take the values of my faders (label.text= [NSString stringWithFormat:@"%i", slvalue];)

but like this if I move my labels don't follows my faders values...

how can I do to my labels follows my faders values?

MY CODE

- (void)viewDidLoad
{
   [super viewDidLoad];    
   for(int i=0; i< 20; i++){
    //sliders
    UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(1+i*50, 450, 300, 10)] autorelease];
    slider.minimumValue = 1;
    slider.maximumValue = 100;
    slider.continuous = YES;
    [self.view addSubview:slider];
    slider.transform = CGAffineTransformRotate(slider.transform, 270.0/180*M_PI);
    [slider setThumbImage:thumbImage0 forState:UIControlStateNormal];
    [slider release];
    // label
    UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(1+i*50, 640, 40, 15)];
    int slvalue = slider.value;
    label.text= [NSString stringWithFormat:@"%i", slvalue];
    [self.view addSubview:label];
    [label release];

}

New issue :

mofified

I do this :

i put an IBAction in my FirstViewController.m

- (IBAction)sliderUpdate:(UISlider *)sender {
self.label.text = [NSString stringWithFormat:@"%g", sender.value];
}

and put this in my viewDidLoad (FirstViewController.m)

{
[super viewDidLoad];    
for(int i=0; i< 20; i++){
//sliders
UISlider *slider = [[[UISlider alloc] initWithFrame:CGRectMake(1+i*50, 450, 300, 10)] autorelease];
slider.minimumValue = 1;
slider.maximumValue = 100;
slider.continuous = YES;
[slider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slider];
slider.transform = CGAffineTransformRotate(slider.transform, 270.0/180*M_PI);
[slider setThumbImage:thumbImage0 forState:UIControlStateNormal];
[slider release];
// label
UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(1+i*50, 640, 40, 15)];
int slvalue = slider.value;
label.text= [NSString stringWithFormat:@"%i", slvalue];
[self.view addSubview:label];
[label release];
}

i have this error in my (IBAction)sliderUpdate: Property 'label' not found on object of type 'FirstViewController *'

2

2 Answers

1
votes

If you control drag from a slider to the implementation of your UIViewController, it will set up an IBAction method that is called whenever the value is changed. In this method, simply set the text of your label. No need to make a special subclass.

The code will end up looking something like this:

- (IBAction)setLabel:(UISlider *)sender {
    self.myLabel.text = [NSString stringWithFormat:@"%g", sender.value];
}

EDIT: It sounds like you are making the sliders programmatically. In that case, you're going to have to call addTarget:action:forControlEvents: on each one to set up the target-action. (The control event will be UIControlEventValueChanged.)

Also, as far as knowing what slider goes to what label, you could make 2 NSArrays in which the ith slider in one array matches the ith label in the other array.

0
votes

Drag and drop or programetically create one UILabel and UISlider ,then in the Slider use this for slider action

-(IBAction)slider:(id)sender {
NSString *s=[NSString stringWithFormat:@"%f",slider.value]; 
[label setText:s];   }

So whenever you change your slider your label value also changes.I hope you got it ryt?