I'm trying to create a push-on-push-off-like button like this one, although my code is a little different:
UIButton* alertsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
alertsButton.frame = CGRectMake(170, 43.5, 130, 130);
[alertsButton.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]];
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateHighlighted];
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateNormal];
[alertsButton setTitle:NSLocalizedString(@"Alerts", nil) forState:UIControlStateSelected];
[alertsButton addTarget:self action:@selector(toggleAlerts:)forControlEvents:UIControlEventTouchUpInside];
-(void)toggleAlerts:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.selected) {
button.selected = NO;
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]];
NSLog(button.titleLabel.text);
} else {
button.selected = YES;
[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"Novecento wide" size:22]];
NSLog(button.titleLabel.text);
}
}
When I push the button, the background changes as expected but the label disappears although it's text is displayed in the debug console. I tried this without results :(