Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The UISwitch object offers a simple ON/OFF toggle that lets users choose a Boolean value. (The switch internationalizes to 1/0 for most non-English localizations.) The switch object contains a single (settable) value property, called on. This returns either YES or NO, depending on current state of the control. You can programmatically update a switch’s value by changing the property value directly or calling setOn:animated:, which offers a way to animate the change.
- (void) didSwitch: (UISwitch *) theSwitch
{
self.title = [NSString stringWithFormat:@"%
theSwitch.on ? @"On" : @"Off"];
}
- (void) viewDidAppear: (BOOL) animated
{
// Create the switch
UISwitch *theSwitch = [[[UISwitch alloc] init] autorelease];
// Trigger on value changes
[theSwitch addTarget:self action:@selector(didSwitch:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:theSwitch];
// Initialize to "off"
theSwitch.on = NO;
self.title = @"Off";
}