我有一些按钮,当按下时,它们从普通按钮变为带有勾号图片的按钮。我遇到的问题是,当按下一个单独的按钮时,我无法将一个勾选的按钮返回到未按下的按钮。这有可能吗?
以下是我的按钮:
- (IBAction)pickCat:(UIButton *)sender {
[self deselectAllButtons];
((UIButton *)sender).selected = true;
UIImage *btnImage1 = [UIImage imageNamed:@"white - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = @"WHITE (Hi-Po)";
}
- (IBAction)pickCatb:(UIButton *)sender {
[self deselectAllButtons];
((UIButton *)sender).selected = true;
UIImage *btnImage1 = [UIImage imageNamed:@"red - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = @"RED (Significant)";
}
- (IBAction)pickCatc:(id)sender {
UIImage *btnImage1 = [UIImage imageNamed:@"yellow - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = @"YELLOW (Serious)";
}
- (IBAction)pickCatd:(id)sender {
UIImage *btnImage1 = [UIImage imageNamed:@"green - on.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = @"GREEN (Important)";
}
- (void)deselectAllButtons
{
// assuming you have a reference to all your buttons
self->Btn1.selected = false;
self->Btn2.selected = false;
}
答案 0 :(得分:0)
我喜欢为每个州设置不同的图像:正常和选择。看起来您正在使用Interface Builder,并且使用不同的图像为正常状态和选定状态设置按钮非常容易。
然后在您的代码中,您可以执行以下操作:
- (IBAction)pickImage:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.locLabel.text = @"Red";
}
- (IBAction)pickImageb:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.locLabel.text = @"Red";
}
- (void)deselectAllButtons
{
// assuming you have a reference to all your buttons
self.btn1.selected = false;
self.btn2.selected = false;
...
}
如果您希望能够在将所有按钮更改为已选中图像之前重置所有按钮,则必须引用您想要影响的所有其他按钮。如果您想获得幻想,请查看Outlet Collections,这样您就可以轻松地遍历IB文件中的一系列按钮,并将其选定状态设置为false。
更新:
以下是您的代码的外观:
- (IBAction)pickCat:(UIButton *)sender {
[self deselectAllButtons];
// no need to cast since you changed your method signature.
// the system now expects sender to be a button
// ((UIButton *)sender).selected = true;
sender.selected = true;
// no need to set the image explicitly. if you did things correctly
// in IB, you've already told the button which image to use when
// selected, and which image to use when not (default). when you set
// the line above (sender.selected = true), that will tell the button
// to go use the image that you added to the button's selected state in IB
// UIImage *btnImage1 = [UIImage imageNamed:@"white - on.png"];
// [sender setImage:btnImage1 forState:UIControlStateNormal];
self.catLabel.text = @"WHITE (Hi-Po)";
}
- (IBAction)pickCatb:(UIButton *)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = @"RED (Significant)";
}
- (IBAction)pickCatc:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = @"YELLOW (Serious)";
}
- (IBAction)pickCatd:(id)sender {
[self deselectAllButtons];
sender.selected = true;
self.catLabel.text = @"GREEN (Important)";
}
- (void)deselectAllButtons
{
// 2 things:
// 1) I'm not sure about self->Btn1. why not self.Btn1?
// 2) it looks like you have 4 buttons (catA, catB, catC, catD). if so, you need to set the selected state for all of them to false
self->Btn1.selected = false;
self->Btn2.selected = false;
self->Btn3.selected = false;
self->Btn4.selected = false;
}