How to use UIStepper to save settings

This tutorial has been updated for X code 7.3 & swift 2.2

Stepper can be used for setting fine controls. For example opacity for image editing, & other image editing options.

  1. First lets set up the x code project as usual. Create new single view application X code project. Set the values as follows and save it on your desktop.

new xcode project

 

2. Go to main storyboard and select view controller. Set its size iPhone 4-inch in attribute section.

size iPhone 4 inch

 

3. Add label & stepper to the main storyboard. Change labels text to 5

add label & stepper

 

4. Connect stepper as outlet to swift file

stepper as outlet to swift file

 

5. Connect stepper as action to swift file

stepper as action to swift file

 

6. Connect label as outlet to swift file

label to swift file

 

Add following code in view did load

override func viewDidLoad() {
    super.viewDidLoad()
    // 1
    stepper.wraps = false

    // 2
    stepper.autorepeat = true
    stepper.continuous = true

    // 3
    stepper.tintColor = UIColor.redColor()

    // 4
    stepper.minimumValue = -10
    stepper.maximumValue = 10

    // 5
    stepper.value = 5

    // Do any additional setup after loading the view, typically from a nib.
}
  1. Wraps : If its true then if you increment after maximum value then minimum value appears.
  2. Autorepeat & continuous : Both do potentially same thing with small difference. If you click and hold then you can either increase or decrease value consistently.
  3. Tint color : You can change default color of stepper
  4. Minimum & maximum value : This is very self explanatory
  5. Value : This would set initial value. If you don’t set this then default value is zero.

Add following code in stepper as action

@IBAction func stepperValueChanged(sender: UIStepper) {

    numLabel.text = "\(Int(sender.value))"

}

Build and run the app with iPhone 5s. Now test the stepper

Screen Shot 2015-03-08 at 5.02.00 pm
Download X code file
Challenge 1: Change increment from 1 to 0.1
Challenge 2: Save value with NSUserdefaults

Screen Shot 2015-03-08 at 5.04.07 pm
Download X code file

Total 0 Votes
0

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?