Daniele DonzelliDaniele DonzelliDaniele DonzelliDaniele Donzelli
  • HOME
  • iOS
  • ARDUINO
  • ETHICAL HACKING
  • ABOUT

UIStepper Tutorial

    Home iOS UIStepper Tutorial

    UIStepper Tutorial

    By Dany | iOS | 0 comment | 21 June, 2016 | 0

    In this tutorial i’ll show you how to create (Interface builder and programmatically) and how to use UIStepper in a iOS application.
    Create a new Xcode Project, new Single View Application

    uistepper
    In Product Name insert UIStepperTutorial and click Next

    uistepper

    Interface Builder

    From objects library drag: UIStepper and two UILabel.
    In the first label we will show the UIStepper value created by Interface Builder, in the second label, one created programmatically, then write 0 in the labels

    uistepper

    Now create an IBOutlet for the labels and for the stepper and an UIAction for the stepper.
    You can do this holding down ctrl key and drag to editor view

    uistepper

    Now we set the stepper value when it changes

    ViewController.swift
    Swift
    1
    2
    3
    4
    5
    @IBAction func ibStepperSetValue(sender: UIStepper) {
            
        let stepperValue = Int(sender.value)
        ibStepperValue.text = String(stepperValue)
    }


    Programmatically

    To create an UIStepper, we declare a progStepper variable in our ViewController

    ViewController.swift
    Swift
    1
    var progStepper: UIStepper!

    Then we add a function that will be called in the viewDidLoad method

    ViewController.swift
    Swift
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    func createProgStepper() {
        progStepper = UIStepper()
        progStepper.frame = CGRectMake(140, 270, 0, 0)
        progStepper.minimumValue = 0
        progStepper.maximumValue = 100
        progStepper.autorepeat = true
        progStepper.continuous = true
        progStepper.tintColor = UIColor.redColor()
        self.view.addSubview(progStepper)
    }

    • Initialize an UIStepper object
    • Set the position (x: 140, y: 270) and the stepper size default (0,0)
    • Set the minimum and maximum value
    • Set autorepeat to true (If true, the user pressing and holding on the stepper repeatedly alters value)
    • Set continuous to true (If true, value change events are sent immediately when the value changes during user interaction. If false, a value change event is sent when user interaction ends)
    • Set the color to red
    • Add the stepper as a subview of the view

    Now we create a function that will be called when the stepper is clicked.
    The code of this function is the same that we wrote for the stepper created with Interface Builder, will set the stepper value when it changes

    ViewController.swift
    Swift
    1
    2
    3
    4
    func progStepperSetValue(sender: UIStepper!) {
        let stepperValue = Int(sender.value)
        progStepperValue.text = String(stepperValue)
    }

    This is the code for the viewDidLoad method

    ViewController.swift
    Swift
    1
    2
    3
    4
    5
    6
    7
    8
    9
    override func viewDidLoad() {
        super.viewDidLoad()
            
        // Create the stepper
        createProgStepper()
            
        // Set the stepper value when it changes
        progStepper.addTarget(self, action: "progStepperSetValue:", forControlEvents: .ValueChanged)
    }

    If you want know more about UIStepper you can refer to the official documentation.

    Download the complete code here.

    VIDEO OF THIS TUTORIAL

    swift, uistepper
    Dany

    Dany

    I'm tech enthusiast, i love learning and experimenting new things. My main interests are iOS Development, Arduino and Ethical Hacking, but i also like to experiment other languages like Python, Java, PHP and Android Development.

    More posts by Dany

    Related Post

    • UISlider Tutorial

      By Dany | 0 comment

      In this tutorial i’ll show you how to create (Interface builder and programmatically) and how to use UISlider in an iOS application. Create a new Xcode Project, new Single View Application In Product Name insertRead more

    • UISwitch Tutorial

      By Dany | 1 comment

      In this tutorial i’ll show you how to create (Interface builder and programmatically) and how to use UISwitch in an iOS application. Create a new Xcode Project, new Single View Application In Product Name insertRead more

    • UITextField Tutorial

      By Dany | 0 comment

      UITextField is the component that is used to get user input. In this tutorial i’ll show you how to create an UITextField, in Interface Builder and programmatically, then we will use the UITextFieldDelegate methods. WeRead more

    • UIButton Tutorial

      By Dany | 0 comment

      In this tutorial i’ll show you how to create an UIButton in an iOS application. You can create a button in two ways, programmatically and with Interface Builder. Create a new Xcode Project, new SingleRead more

    Recent Posts

    • 7 August, 2016
      1

      Blind SQL Injection – Boolean based

    • 15 July, 2016
      0

      Controlling LED brightness with a potentiometer

    • 4 July, 2016
      0

      SQL Injection – Union Based

    • 21 June, 2016
      0

      UIStepper Tutorial

    Archives

    • August 2016
    • July 2016
    • June 2016
    • May 2016
    • April 2016

    Categories

    • Arduino
    • Ethical Hacking
    • iOS

    Recent Posts

    • 7 August, 2016
      1

      Blind SQL Injection – Boolean based

    • 15 July, 2016
      0

      Controlling LED brightness with a potentiometer

    • 4 July, 2016
      0

      SQL Injection – Union Based

    Categories

    • Arduino
    • Ethical Hacking
    • iOS
    © 2016 Donzelli Daniele | All Rights Reserved
    • HOME
    • iOS
    • ARDUINO
    • ETHICAL HACKING
    • ABOUT
    Daniele Donzelli