我在表格视图中有三个按钮图像,我想检查它们之间的条件。当我单击按钮1时,表示背景图像变为蓝色。同时单击按钮1,它将变为正常状态的白色。其他两个按钮相同。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath![button3 condition][3]
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
UIButton *button1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
button1.tag = 1;
[button1 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button1 setSelected:true];
[cell.contentView addSubview:button1];
UIButton *button2;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e"ofType:@"png"]] forState:UIControlStateNormal];
button2.tag = 1;
[button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button2 setSelected:true];
[cell.contentView addSubview:button2];
UIButton *button3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
button3.tag = 1;
[button3 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[button3 setSelected:true];
[cell.contentView addSubview:button3];
return cell;
}
我的条件是:单击按钮1时,按钮3不应更改。当单击按钮3时,意味着按钮1不应更改。按钮2可以在两种情况下选择。
- (void)radiobtn:(UIButton *)button
{
if(button.tag == 1)
{
[button setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 2)
{
[button setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected];
}
if(button.tag == 3)
{
[button setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected];
}
}
谁能帮我编码。
最佳答案
首先,您可以为每个按钮的样式设置不同的标签
button.tag == 1,
button.tag == 2,
button.tag == 3
然后,您可以像这样编写
radiobtn
操作。-(IBAction) radiobtn:(id)sender
{
UIButton *yourBtn = (UIButton *)[self.view viewWithTag:[sender tag]];
if(yourBtn.tag == 1) {
[yourBtn setImage:[UIImage imageNamed:@"lblue.png"] forState:UIControlStateSelected];
}
else if(yourBtn.tag == 2){
[yourBtn setImage:[UIImage imageNamed:@"eblue.png"] forState:UIControlStateSelected];
}
else{
[yourBtn setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected];
}
}