UIStepper was introduced in iOS 5.0.It provides an option for the user to increment or decrement values.Now we are going to create a project with UIStepper which displays increment or decrement values.
- open Xcode and Select Create a new Xcode project –>Single View Application.
- Enter the product name as ‘UIStepperExample‘,this will remain as your project name.
- Enter company identifier,organizaton name,bundle identifier.
- Check option Use Automatic Reference Counting and uncheck option Use Storyboards.
- ctrl + drag from UIStepper to interface section to create an outlet for UIStepper and name it as ‘stepperOutlet‘.
- ctrl+drag from UILabel to interface section to create an outlet for UILabel and name it as ‘stepperLabel‘.
- ctrl+drag from UIStepper to interface section to create an Action for UIStepper and name it as ‘stepperClicked‘.
- Now add this code to ViewController.m file inside the ‘viewDidLoad‘ method.
self.stepperOutlet.minimumValue = 0;
self.stepperOutlet.maximumValue = 1000;
self.stepperOutlet.stepValue = 10;
self.stepperOutlet.autorepeat = YES;
self.stepperOutlet.continuous = YES;
self.stepperLabel.text = [NSString stringWithFormat:@"Value:%d",0];
Minimum Value:It is the minimum value the UIStepper will start from.Here we our minimum value is ‘0‘.
Maximum Value:It is the maximum value of the UIStepper can reach.Here our maximum value is ‘1000‘.
Step Value:It is the value by which UIStepper will increment or decrement each time when we press ‘+‘ or ‘–‘ button.
Autorepeat:If ‘YES’ the UIStepper will continuously increment or decrement the value when the ‘+‘ or ‘–‘ button is press and hold.
Continuous:If ‘YES’ the value change events are send immediately when the user clicks ‘+‘ or ‘–‘ button.If ‘NO’ the value changing events are send when the user interaction ends.
- Now add this code inside the method ‘stepperClicked‘,
int stepperIntValue = self.stepperOutlet.value;
self.stepperLabel.text = [NSString stringWithFormat:@"Value:%d",stepperIntValue];