iOS7 Controls – UISwitch

When we need to state a boolean object in iOS apps, UISwitch is best way to do it. It’s quiet simple. It’s whether ON or OFF. At the end of this article you may find public repository for demo of this control but for now let me define how i got up to that point.

First of all, fire up your Xcode and create a new Single View Application. I named is as iOS7UISwitchTutorial. Then you should fill out the Organization Name, Company Identifier. Then I chose iPhone in Devices area.

Now it’s time to design our interface. Go to Storyboard and design your screen like this:


When you finished your interface, you should enable your Assistant Editor. Assistant Editor is the tuxedo sign at upper right side of your Xcode. basically it splits your screen half. This part is very crucial (and yet quiet easy) and if you intend to develop serious iOS applications because you’re about to connect your interface elements with your back-end code. When your screen splits in 2 pieces, open your storyboard at one side, and open your ViewController.m file at another side. Then right click on your TextField and drag it to the @interface section of ViewController.m file while you keep pressing your mouse’s right button. When you’re done, you need to create an outlet for your TextField.


Do the same for your UISwitch:


you should do the same thing for your button but with 2 tiny differences. You should drag it under @implementation in your ViewController.m file. And instead of creating an outlet, this time you’re creating an action.


Next, we should implement buttonClicked action that we created. As you may guess, action gives us the ability to change something while we interact with button. But outlet just gives us the ability to manipulate controls from back-end. In our case, we’re going to change state of TextField and UISwitch by coding. And when we do that coding in our action, it wait for us to click on the button to make that manipulation.

Here is the implementation of buttonClicked method:

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *myTextField;
@property (strong, nonatomic) IBOutlet UISwitch *mySwitch;


@implementation ViewController
- (IBAction)buttonClicked:(id)sender {
    if ([self.mySwitch isOn]) {
        self.myTextField.text=@"The switch is off";
        [self.mySwitch setOn:NO animated:YES];
        NSLog(@"switch turned off");
    else {
        self.myTextField.text=@"The switch is ON";
        [self.mySwitch setOn:YES animated:YES];
        NSLog(@"switch turned on");

- (void)viewDidLoad
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    if (self.mySwitch.isOn) {
        NSLog(@"switch is on at start");


And here’s the git repository. You may download the code here:

I’m not sure if the repository is public, you may warn me about if it’s still private or public. Any comments also welcome and let me know if there’s anything i can help.


Yorum bırakın

Filed under iOS App Development

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın: Logosu hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )


Connecting to %s