gpt4 book ai didi

ios - 如何检查三个按钮条件

转载 作者:行者123 更新时间:2023-11-29 02:18:32 26 4
gpt4 key购买 nike

我有三个按钮图像,我想检查它们之间的条件。当我单击按钮 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];
}
}

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

最佳答案

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

根据按钮按下交替显示图像

-(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;
}
}

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


为每个按钮添加 Action

[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. 为每个按钮添加 Action
  2. 相应地标记每个按钮
  3. 设置每个按钮的初始图像以匹配条件

关于ios - 如何检查三个按钮条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28447753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com