Crossed Ads

Wednesday, 14 June 2017

How to implement Switch control in Objective-C with Xcode 8 ? (Blog)

Switch control in iOS (Objective-C)


In this tutorial, we will see how to implement Switch control in iOS application using Objective-C Language.

Switch is a control which offers to choose ON/OFF state. To do so, UISwitch class is used to implement switch control into your iOS project. This control is basically used to turn On or Off functionality of a particular task.

Example - Turning ON/OFF WiFi or Airplane Mode or Mobile Data in Settings of your iPhone or iPad.


Following are the steps to implement Switch control:

Step 1: Open Xcode and create new project by clicking on Single View Application template under iOS tab as shown in below image and click Next button.



Step 2: Enter the product name as SwitchDemo, 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 UISwitch & UILabel into the View Controller.
On your project, click on main.storyboard file available in the project navigator, and then select the UISwitch from object library, drag into the superview of View Controller, it will look like below.  




Moreover, Switch control is by default at ON state. Manually, you can set to OFF from Attribute Inspector by selecting Switch control.

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

Step 4: Declaring property for Progress View & Label.

Select the Assistant Editor and create IBOutlet & IBAction property for UISwitch control by holding Ctrl button. It will open up a popup view like below then enter the Name as ‘switchButton’ and ‘switchSwipped’ respectively and click Connect.  



Both property (IBOutlet & IBAction) is defined because on action event of Switch control some text should be displayed on condition of its state which will be known using isOn method of switch by calling its outlet property as the same can be 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, all code will be written in action method of switch control where we check the state of switch control by accessing its name of outlet property using isOn property in if statement. Thereafter, some message will be displayed like “Switch is ON” will be displayed on Label and if that not On then “Switch is OFF” will be displayed on Label. 



That’s the complete code for Switch control program.


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:



4 comments:

  1. I would like to thank you for the efforts you have made in writing this article, Its good and Informative.
    ios online training hyderabad

    ReplyDelete
    Replies
    1. Thanks for your supportive comment!
      I am always be glad to provide more content to help for those who are keen to learn iOS Development.

      Delete
  2. Thanks for your supportive comment!
    I am always be glad to provide more content to help for those who are keen to learn iOS Development.

    ReplyDelete
  3. Thanks for your supportive comment!
    I am always be glad to provide more content to help for those who are keen to learn iOS Development.

    ReplyDelete