按下其他按钮后不能更改uibutton的图像

我有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)时出现问题。


你可以为此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;
}
链接地址: http://www.djcxy.com/p/96255.html

上一篇: can't change image of uibutton after pressing other buttons

下一篇: UIButton – what is the default image highlight color