Crossed Ads

Sunday, 18 June 2017

How to implement Stepper control in iOS using Objective-C with Xcode-8 ? (Blog)

Stepper control in iOS (Objective-C)


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:

Value
It is the value which is getting printed as its initial value. Its default value of 0.

Minimum Value
It is the value from where it gets started. Its default min. value is 0.

Maximum Value
It is the value where it gets end. Its default max. value is 100.

Step Value
It is the value which will increment/decrement each time when +/- pressed. Its default value is 1.

Autorepeat
The value will continuously increment/decrement when +/- pressed & hold. Its default value is YES/TRUE.

Continuous
The value change events are send immediately when +/- pressed. Its default value is also YES/TRUE.

Wrap
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 = 2;
self.stepper.minimumValue = 1;
self.stepper.maximumValue = 100;
self.stepper.stepValue = 10;

self.stepper.autorepeat = YES;
self.stepper.continuous = YES;
self.stepper.wraps = NO;

In the same manner, Label will also be placed using drag & drop method into View Controller to show the value of Stepper control.


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:
https://www.youtube.com/watch?v=SajI2Zp-iqo



2 comments:

  1. Thanks for your compliment & support!
    I'll more to you in more easiest way.

    ReplyDelete
  2. Casino Resort in Maricopa - Mapyro
    Find the cheapest 화성 출장샵 and 인천광역 출장안마 quickest way to get from 동해 출장마사지 Casino Resort Casino 오산 출장마사지 Resort to Jardin City Inn & Suites by Wyndham 강원도 출장마사지 Phoenix International Airport in Maricopa.

    ReplyDelete