I’m trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS.
The code I’m using is
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
That works fine for now.
But my problem is, that when I’m toggling on, I press it on, then it is popping off again and then finally on.
The app works, but that is really ugly, though.
I firstly set the “highlighted” image to on. So when I highlight the button, it is on and that popping to on. That works fine. But when I turn it off again, the problem is the same, in the reverse direction.
I tried to put that code:
- (IBAction)btnAll:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage[UIImage imageNamed@"off.png"] forState:UIControlStateHighlighted];
}
else
{
[button setImage[UIImage imageNamed@"on.png"] forState:UIControlStateHighlighted];
}
button.selected = !button.selected;
}
But as long button.selected = !button.selected there is no difference.
So it won’t make any change.
I also tried to trigger the IBAction on »Touch Down« but you can imagine how frustrating this will look like.
Has anybody got a solution for that problem?
Did anybody struggle with that one too?
Greets, thanks a lot
Julian
I’ve had a similar problem to this before, the button works a little strangely when tapping. Try this code and let me know if it works
When you tap and hold on a button the state is actually Highlighted & Selected so you need an image for both Highlight and selected state.