如何检查三个按钮条件

时间:2015-02-11 06:19:35

标签: ios objective-c iphone uiimageview uibutton

我有三个按钮图像,我想检查它们之间的条件。当我点击按钮1表示背景图像变为蓝色。同时我点击按钮1它将移动到正常状态白色。对于另外两个按钮也是如此。对于该过程,我正在使用此代码。 [tempBtn setSelected:! tempBtn.selected];

ButtonImageSelected = [UIImage imageNamed:@"lblue.png"];
ButtonImage = [UIImage imageNamed:@"l.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setBackgroundImage:ButtonImage forState:UIControlStateNormal];

button1.tag = 1;
[button1 setBackgroundImage:ButtonImageSelected
                   forState:UIControlStateSelected];
button1.userInteractionEnabled = YES;
[button1 addTarget:self
              action:@selector(button1Selector:)
    forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button1];

另外两个按钮与按钮1相同。

我的条件是:当点击按钮1时,按钮3不应该改变。当点击按钮3意味着按钮1不应该改变。按钮2可以在两个条件中选择。我使用下面的代码检查条件,但它不能正常工作。

- (void)select_id:(UIButton *)tempBtn {
  if (tempBtn.tag == 1) {
    button3.userInteractionEnabled = NO;
    [tempBtn setSelected:!tempBtn.selected];
  } else if (tempBtn.tag == 3) {
    button1.userInteractionEnabled = NO;
    [tempBtn setSelected:!tempBtn.selected];
  }
}

帮我看看如何检查条件。在上面的方法中。

2 个答案:

答案 0 :(得分:1)

我相信你要完成的是让每个按钮在按下时改变状态。一旦定义了按钮,就可以确保它们为每个状态交替显示图像。

基于按钮的交替图像

-(IBAction)buttonPressed:(id)sender {
    switch ([sender tag]) {
        case 1:
            if ([UIImagePNGRepresentation(button1.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:@"default1. png"])]) {
                [button1 setImage:[UIImage imageNamed:@"selected1.png"] forState:UIControlStateNormal];
            } else {
                [button1 setImage:[UIImage imageNamed:@"default1.png"] forState:UIControlStateNormal];
            }
            break;
        case 2:
            if ([UIImagePNGRepresentation(button2.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:@"default2. png"])]) {
                [button2 setImage:[UIImage imageNamed:@"selected2.png"] forState:UIControlStateNormal];
            } else {
                [button2 setImage:[UIImage imageNamed:@"default2.png"] forState:UIControlStateNormal];
            }
            break;
        case 3:
            if ([UIImagePNGRepresentation(button3.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:@"default3.png"])]) {
                [button3 setImage:[UIImage imageNamed:@"selected3.png"] forState:UIControlStateNormal];
            } else {
                [button3 setImage:[UIImage imageNamed:@"default3.png"] forState:UIControlStateNormal];
            }
            break;
        default:
            break;
    }
}

注意:确保将操作添加到每个按钮,并更改图像的名称


将动作添加到每个按钮

[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

确保您已完成以下操作:

  1. 为每个按钮添加了操作
  2. 相应地标记每个按钮
  3. 设置每个按钮的初始图像以匹配条件

答案 1 :(得分:0)

试试这个。在此之前,请确保标签适用于每个按钮

- (void)select_id:(UIButton *)tempBtn {
  if (tempBtn.tag == 1) {
    button3.userInteractionEnabled = NO;
    [tempBtn setSelected:!tempBtn.selected];
  } else if (tempBtn.tag == 3) {
    button1.userInteractionEnabled = NO;
    [tempBtn setSelected:!tempBtn.selected];
  }
  else{
     button1.userInteractionEnabled = YES;
     button2.userInteractionEnabled = YES;
    [tempBtn setSelected:!tempBtn.selected];


  }
}