How To Switch Work In iPhone

This is the very simple example. In this example we will see how to Switch function worked.

Step 1: Create a View base application using template. Give the application name “SwitchON”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand classes and notice Interface Builder created the SwitchONViewController class for you. Expand Resources and notice the template generated a separate nib, SwitchONViewController.xib, for the “SwitchON”.

Step 4: Open the SwitchONViewController.h file and make the following changes in the file.


#import <uikit/UIKit.h>

@interface SwitchONViewController : UIViewController {

IBOutlet UILabel *display;
IBOutlet UISwitch *switch1;


@property (nonatomic,retain) UISwitch *switch1;

@property (nonatomic,retain) UILabel *display;




Step 5: Double click the SwitchONViewController.xib file and open it to the Interface Builder. Now first drag the switch from the library and place it to the view window and drag the label from the library and place it to the view window. Select the label from the view window and bring up attribute inspector, give the Text name “Please Switch me OFF!” . Select the switch from the view window and bring up Connection Inspector, connect Value Changed to the File’s Owner icon and select messagedisplay:, Connect File’s Owner icon to the switch and select switch1. Now save it close it and go back to the Xcode.

Step 6: In the SwitchONViewController.m file make the following changes.


NSString *message1 = [[NSString alloc] initWithFormat:@"Please switch me OFF!"];
NSString *message2 = [[NSString alloc] initWithFormat:@"Please switch me ON!"];

if (switch1.on){
display.text = @"Please switch me OFF!";
[message1 release];
else {
display.text = message2;
[message2 release];




Step 7: Now compile and run the application in the simulator.

You can Download SourceCode from here SwitchON

Leave a Comment:

1 comment
vineet sharma says February 26, 2011

hello Sushant,

i want to consume webservice in iphone/ipad .can you please tell me in detail how to consume SOAP web service in iphone. i tried with WSDL2OBJC but i got lots of errors 🙁
plzz help

thanks in advance

vineet sharma

Add Your Reply