我有2个按钮与被按下的图像保持一致,直到它们被释放,直到按下另一个按钮,然后图像返回到正常图像我的代码是这样的:
- (IBAction)button1_touch:(UIButton *)sender
{
[Button2 setImage:[UIImage imageNamed:@"b2Released.png"] forState:UIControlStateNormal];
[Button1 setImage:[UIImage imageNamed:@"b1Pressed.png"] forState:UIControlStateNormal];
}
- (IBAction)button2_touch:(UIButton *)sender
{
[Button1 setImage:[UIImage imageNamed:@"b1Released.png"] forState:UIControlStateNormal];
[Button2 setImage:[UIImage imageNamed:@"b2Pressed.png"] forState:UIControlStateNormal];
}
上面的代码在应用程序启动时工作正常,行为是预期按下两个按钮,图像正确更改,但当我按下其他按钮而不是那两个按钮时,“Button2”一旦获得“B2Pressed.png”图像,它永远不会返回到它发布的图像,我在button1_touch中写了一个nslog,它按预期打印,而Button1获取其按下的图像,但是Button2没有得到它发布的图像,就像UiButton的方法setImage被破坏了我按下除这两个按钮之外的其他按钮,其他按钮独立于这两个按钮,对我来说这种行为完全没有任何意义,可能是xcode坏了吗?我正在研究xcode 7.2
编辑: 当我按下另一个按钮时,如果该按钮分配了一个动作选择器(UIControlEventTouchUpInside),则会出现问题。
答案 0 :(得分:2)
您可以为此目的使用selected
状态的UIButton:
- (void)viewDidLoad {
// ...
// the place where you do buttons setup
[button1 setImage:[UIImage imageNamed:@"b1Released"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"b1Pressed"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:@"b2Released"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"b2Pressed"] forState:UIControlStateSelected];
}
- (IBAction)firstButtonTouch:(UIButton *)sender {
button1.selected = YES;
button2.selected = NO;
}
- (IBAction)secondButtonTouch:(UIButton *)sender {
button1.selected = NO;
button2.selected = YES;
}