Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Building and Using Controls > Working with Switches and Steppers

Working with Switches and Steppers

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";
}


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint