Creating programmatically a switch (UISwitch)

May 8th, 2009 by Chris Leave a reply »

UISwitch is one of the simplest structure of UIKit. To create it programmatically, code:

	UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 0, 0)];
Above line allocates an UISwitch and sets its frame. UISwitch has fixed dimensions (about 95 px long, 27px high), so you have to set the coordinates for top-left corner. Width and height are ignored, you can’t make switch smaller or bigger by changing the frame.

To add it to your view you should use this code:

	[self.view addSubview:mySwitch];

Be default switch is set to Off. You are able to change it’s state programmaticaly using setOn method:

	[mySwitch setOn:YES animated:YES];

To switch it back to Off use:

	[mySwitch setOn:NO animated:YES];

In both above cases UISwitch changes it state with animation (animated:YES), use animated:NO to change state without animation.

To check UISwitch current state use on property, example:

	if (mySwitch.on) NSLog(@"switch state: on");
	else NSLog(@"switch state: off");

To prevent changing the UISwitch state set the enabled property to NO:

mySwitch.enabled = NO; // or YES to reenable it

As I said before UISwitch is very simple structure – any action – touching it causes UISwitch to change it’s state, so to call any method by changing the UISwitch you have to add a target for ValueChanged event:

	[mySwitch addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventValueChanged];
or
	[mySwitch addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventValueChanged];
If you need a id to mySwitch object.
That’s all about UISwitch. More can be found in UISwitch Class Reference in documentation.
Advertisement

10 comments

  1. Bill says:

    Thanks, just what I was looking for.

  2. Leo Malina says:

    You must be happy. My wife promised to cut off my dangly bits if I didn’t quit visiting you web site.

  3. lpn says:

    lpn
    Hello.This post was really remarkable, particularly because I was investigating for thoughts on this topic last Thursday.

  4. What a great post. I actually like reading these kinds or posts. I can?t wait to view what others have to say.

  5. People, I am somewhat skeptical when it comes to deals on the internet that look too good to be real, but I went with this iPhone 4 promotion that is currently going on.

    They sent me an iPhone 4 (which I just received the other day). The catch seems to be that I have to fill out this survey about it. It’s pretty long but if you think about it – I get to keep the iPhone! This seems a super deal.

    If you are interested in this, their site has all the info: http://iphone4testers.blogspot.com

  6. Mathew says:

    Thanks! exactly what i wanted.
    if i may ask, whats the difference between ‘action:@selector(myMethod)’ and ‘action:@selector(myMethod:)’, what does ‘:’ do?
    Cheers

    • Chris says:

      when you add colon (:), the method received the reference to the object that called this method

      -(void)myMethod:(id)sender {
      // sth here
      // and you can also send methods to the “sender” – object that called this method, for example valid something, and turn the switch off with uialertview
      }

  7. Vish says:

    Thank you. It helped a lot.

Leave a Reply