Search code examples
objective-ccocoa-touchuiswitch

A UISwitch that is never off


I created a UISwitch with the following code:

CGRect switchFrame = CGRectMake(200, 10, 94, 27);
UISwitch *yesNo = [[[UISwitch alloc] initWithFrame:switchFrame] autorelease];
[yesNo addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];

However, no matter the state of the switch the on property always returns YES, even when it's visually NO.

The event handler looks like this:

-(IBAction) handleSwitch: (UISwitch *) sender{
    self.displayCompleted = sender.on;
}

Which sets the displayCompleted property:

-(void) setDisplayCompleted:(BOOL)newValue{
    displayCompleted = newValue;
    [[self tableView] reloadData];
}

What might be causing this?


Solution

  • I was making a stupid mistake: when recreating the cell after reloadData the UISwitch was created without reflecting the current state of the displayCompleted property.