问题
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
回答1:
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
UIButton *button = (UIButton *)sender;
if(button.selected)
{
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
}
button.selected = !button.selected;
If (button.selected) {
[button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];
} else {
[button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
}
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.
回答2:
Don't manually switch the images around, just set the selected state's image in Interface Builder and swap the selected
property over when the button is tapped.
回答3:
Agree with Jim, your code needs little modification as below...
[button setImage[UIImage imageNamed@"off.png"] forState:UIControlStateNormal];
[button setImage[UIImage imageNamed@"on.png"] forState:UIControlStateSelected];
回答4:
Have two buttons created.
place buttons one above one.
Can set Default & Selected images using Custom Button option in design builder.
-(void)firstButtonClicked {
firstButton.hidden = YES;
secondButton.hidden = NO;
}
-(void)secondButtonClicked {
firstButton.hidden = NO;
secondButton.hidden = YES;
}
回答5:
@Jim - when do u switch the isSelected state? isHighlighted gets called twice for every press. My hacked version of UIButton switch below : -
class ButtonSwitch: UIButton {
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
if allControlEvents == .touchUpInside {
isSelected.toggle()
}
super.sendAction(action, to: target, for: event)
}
}
来源:https://stackoverflow.com/questions/8571485/uibutton-as-switch