Source Freeze

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

UISwitch Tutorial using swift in iOS

December 28, 2022 by Source Freeze 1 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

Comments

  1. Sourav Vashisht says

    December 29, 2016 at 4:12 pm

    when i am click on uiswitch it gives me this problem([switch_button.ViewController switchIsChanged:]: unrecognized selector sent to instance) can you please give me solution of this…

    Reply

Leave a Reply Cancel reply

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

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development nextjs objective-c swift uiwebview Visual Studio plugin

Copyright © 2023 Source Freeze