In this tutorial blog, we will see how to implement Stepper control for iOS application using Objective-C Language.
Stepper is a control which offers the user to increment/decrement a value. It is a two-segment control which consists of + & – symbol used to increase and decrease a value respectively. To do so, UIStepper class is provided to implement stepper control into your iOS project. This control is basically used to mention a numeric value of an object.
Example – In real scenario, it can be seen when you give print from your iPhone/iPad and need to specify how many pages you want to print, then there is stepper used to specify your number of pages to print.
Following are the steps to implement Stepper control:
Step 1: Open Xcode -> click on Create new Xcode project then choose Single View Application template under iOS tab as shown in below image and click Next button.
Step 2: Enter the product name as StepperDemo, select the Language as Objective-C and again click Next button as shown in below image. Later click Create button to create project appear on the next window.
Step 3: Adding UIStepper & UILabel into the View Controller.
On your project, click on main.storyboard file available in the project navigator, and then select the UIStepper from object library, drag into the superview of View Controller, it will look like below.
Stepper control is having following properties:
It is the value which is getting printed as its initial value. Its default value of 0.
It is the value from where it gets started. Its default min. value is 0.
It is the value where it gets end. Its default max. value is 100.
It is the value which will increment/decrement each time when +/- pressed. Its default value is 1.
The value will continuously increment/decrement when +/- pressed & hold. Its default value is YES/TRUE.
The value change events are send immediately when +/- pressed. Its default value is also YES/TRUE.
It is the value which will continuously increment/decrement after reaching its end value or start value. Its default value is NO/FALSE.
The default values of Stepper control can be changed manually from Attribute Inspector as well as using code as mentioned below.
self.stepper.value = 10;
self.stepper.minimumValue = 10;
self.stepper.maximumValue = 100;
self.stepper.stepValue = 10;
self.stepper.autorepeat = YES;
self.stepper.continuous = YES;
self.stepper.wraps = NO;
Step 4: Declaring property for Stepper & Label.
Select the Assistant Editor and create IBOutlet & IBAction property for UIStepper control by holding Ctrl button. It will open up a popup view like below then enter the Name as ‘stepper’ and ‘stepperClicked’ respectively and click Connect.
Both property (IBOutlet & IBAction) is defined because number will be displayed on Label on action event of Stepper control by calling its outlet property to access it value (as seen in below screenshot).
In the same manner, set up the IBOutlet property for Label object & name it as ‘lblStatus’ and then hit ‘Enter’ button.
The all property defined in ViewController.h file will be shown as below:
Step 5: Complete Code in ViewController.m file
In ViewController.m file, displaying value of stepper will be passed to Label in stepper action method as shown in below screenshot.
Step 6: Finally, Build and Run the application where you will get the output like below.
Have Fun !
You can also watch its video on below link: