Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

Switch

The switch can be defined as the UIControl, which provides binary choices to the user either on or off. The state of a switch is managed by properties and methods defined in the UISwitch class, which is the subclass of UIControl.

The UISwitch class is declared as follows.

A switch can have only one state at once, either on or off. When the user tries to change the state of the switch, the valueChanged event is generated, and an action call is sent to the action connection associated with the swift.

We can customize the appearance of the swift by using the properties defined in the UISwitch class.

A switch can be added to the interface using the following steps.

  1. Create the object of the UISwitch class or search for it in the object library and drag the result to the storyboard builder.
  2. Create an outlet of the switch in the ViewController class to customize its appearance at runtime.
  3. Create an action connection method of the switch in the ViewController class, which can be called at runtime when the valueChanged event is triggered for the switch.

Example

Here, we will create a very simple example, in which we will maintain the state of the switch and create a function to which accepts the call back when the state of the switch is changed.

Interface Builder

In this example, we have created a very simple storyboard in which we have use the switch and the label. Here, we will use the label to show the state of the switch, whether it is on or off. The label and switch are connected to the outlets in the ViewController.swift file.

iOS Switch

ViewController.swift

Output:

iOS Switch
Next TopicSegment Control





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA