Tutorial for UISwitch control

UISwitch control, as its name implies is just like a switch  to turn on and off things for the app. You can take things as certain features for the app that user can set according to his/her needs.

In his post I am going to tell you that how can we take advantage of UISwitch in our IPhone app.

                                                              Suppose we have a requirement where we want to show user a password screen before app opens up and this screen appearance is totally dependent on value set by user in settings screen of the app.In this case we can use UISwitch control and saving user preferences based on the UISwitch selection or value.Now lets jump int to the code

Open your .h file and create an IBOutlet for our UISwitch control and declare its IBAction.

@interface ADCViewController : UIViewController
{

    IBOutlet UISwitch *passwordSwitch;
}
    -(IBAction)passwordSwitchValueChanged:(id)sender;

Now, open up your .xib file, drag and drop UISwitch Control to it. Connect your IBOutlet (right click on File Owner, select circle next to our outlet and drag it towards UISwitch control) and set is section for valueChanged event (this will trigger when value of our UISwitch gets changed).

 Open your .m file as its time to write our method written in .h file

-(IBAction)passwordSwitchValueChanged:(id)sender
{
    NSLog(@”%d”,[passwordSwitch isOn]);
    if ([passwordSwitch isOn])
    {
        //set it on
        [passwordSwitch setOn:TRUE];
     
    }
    else
    {
        //set it to off
        [passwordSwitch setOn:FALSE];
    }
}

That’s it you are all done with UISwitch.