Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » uiswitch tutorial

UISwitch Tutorial using swift in iOS

December 28, 2022 by Source Freeze Leave a Comment

UISwitch Tutorial introduction

The iOS switch control is like a real time electronic switch . UISwitch class is used  to create and manage on/off state of the buttons that will work like a switch, this UISwitch on/off state is represented by boolean object, you can see the example of uiswitch in ios settings apps like  Auto-Capitalization, and Auto-Correction, etc.

uiswitch used in the setting application

This ios programming “UISwitch Tutorial” is written in swift so you must need Xcode6 to run this application.

Open the Xcode and create a new single view application,

iOS8 uiswitch tutorial swift  single view applicatoin

Then, enter the product name as “UISwitch Tutorials” next fill out the Organization Name and Organization identifier then select “Swift” language and device family as iPhone and remains the core data field unchecked because we are not using that feature, to create ios switch we can either use a storyboard or create by code instance let’s start our tutorial using storyboard.

ios8 uiswitch tutorials using swift

Adding UISwitch into a storyboard

Then go to the storyboard and drag the ios switch, button and label  to the main view, next change the button title as “Press here to Change UISwitch State”  and label title as “The UISwitch is ON” then the  storyboard should like below.

UISwitch Stoaryboard view

Then go to the ViewController.swift  by selecting the Assistance editor, and create outlet for UISwitch

UISwitch Outlet Created

Next drag the label and create outlet like below,

UILabel for UISwitch change Text

the below code added into your viewcontroller.swift file

@IBOutlet var mySwitch: UISwitch 
@IBOutlet var switchState: UILabel

Next drag the UIButton and create action like below,

UISwitch Button clieck event

UISwitch Coding Part

then we will implement button clicked event like below,

@IBAction func buttonClicked(sender: AnyObject) 
{ 
if mySwitch.on 
{ switchState.text = "UISwitch is OFF" 
println("Switch is on") 
mySwitch.setOn(false, animated:true) 
} 
else 
{ 
switchState.text = "UISwitch is ON" 
println("Switch is off") 
mySwitch.setOn(true, animated:true) 
} 
}

we can change the state of uiswitch by the method of setOn:animated:, if the uiswitch is on once we clicked button it will change the uiswitch state to off, and updates the label text, otherwise the uiswitch state is changed to on and label text updated.

by manually change the state uiswitch by enter the below code into viewDidLoad method,

mySwitch.addTarget(self, action: Selector("switchIsChanged:"), forControlEvents: UIControlEvents.ValueChanged)

When the switch is flipped the UIControlEventValueChanged event is triggered and the stateChanged method will be called.

func switchIsChanged(mySwitch: UISwitch) 
{ 
if mySwitch.on 
{ 
switchState.text = "UISwitch is ON" 
} 
else 
{ 
switchState.text = "UISwitch is OFF" 
} 
}

and the label text updated as per uiswitch state, and go ahead Build and Run “UISwitch Tutorial” application, will see the output like below, and download the uiswitch tutorial source code from here.

uiswitch on off state output

Filed Under: ios-tutorial, swift, UISwitch

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze