UISwitch is one of the simplest structure of UIKit. To create it programmatically, code:
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(50, 100, 0, 0)];
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];
[mySwitch addTarget:self action:@selector(myMethod:) forControlEvents:UIControlEventValueChanged];
I am impressed, I must say. Really rarely do I discovered a blog thats both educational and entertaining, and let me tell you, you have hit the nail on the head. Your blog is important; the issue is something that not enough people are speaking intelligently about. Im really happy that I stumbled across this in my search for something relating to this.
You forgot [myswitch release]
cool! This is what I was looking for! Thanks
look videos watch
Thanks, just what I was looking for.
You must be happy. My wife promised to cut off my dangly bits if I didn’t quit visiting you web site.
lpn
Hello.This post was really remarkable, particularly because I was investigating for thoughts on this topic last Thursday.
You’re right
What a great post. I actually like reading these kinds or posts. I can?t wait to view what others have to say.
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
Thanks! exactly what i wanted.
if i may ask, whats the difference between ‘action:@selector(myMethod)’ and ‘action:@selector(myMethod:)’, what does ‘:’ do?
Cheers
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
}
Thank you. It helped a lot.