iOS - Sliders

Advertisements


Use of Sliders

Sliders are used to choose a single value from a range of values.

Important properties

  • continuous

  • maximumValue

  • minimumValue

  • value

Important method

- (void)setValue:(float)value animated:(BOOL)animated 

Add custom methods addSlider and sliderChanged:

-(IBAction)sliderChanged:(id)sender{
    NSLog(@"SliderValue %f",mySlider.value);
}
-(void)addSlider{
    mySlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 200, 200, 23)];
    [self.view addSubview:mySlider];
    mySlider.minimumValue = 10.0;
    mySlider.maximumValue = 99.0;
    mySlider.continuous = NO;
    [mySlider addTarget:self action:@selector(sliderChanged:) 
    forControlEvents:UIControlEventValueChanged];
}

Update viewDidLoad in ViewController.m as follows

(void)viewDidLoad
{
   [super viewDidLoad];
   [self addSlider];
}

Output

Now when we run the application we'll get the following output.

iOS Tutorial

On dragging the slider the output will be as follows and will print the new value in the console.

iOS Tutorial

Advertisements
Advertisements