gpt4 book ai didi

ios - 如何更改多个按钮图像和条件

转载 作者:行者123 更新时间:2023-11-29 02:15:17 25 4
gpt4 key购买 nike

我在表格 View 单元格中有两个按钮,当我单击第一个按钮时,背景图像更改为所选图像,同时当选择第一个按钮时,我的第二个按钮图像不应更改。我该如何完成这个过程。

UIButton *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];
[cell.contentView addSubview:button1];

UIButton *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 = 2;
[button2 addTarget:self action:@selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];


- (void)radiobtn:(UIButton *)button
{

}

这些按钮位于表格 View 单元格中,第一个按钮选择的图像名称 = 蓝色图像。第二个按钮选择的图像名称 = 红色图像。请帮助我,我是 IOS 新手。

最佳答案

请使用不同的选择器并在不同的函数中执行代码。目前您只使用了一种方法,即 。 radiobtn:(UIButton *){}

或者,如果您想保留相同的选择器,那么您可以检查单击了哪个按钮的标签并相应地执行代码

-(void)radiobtn:(UIButton *)button
{
if(button.tag==1)
{
// perform the code when button 1 is clicked and change the images accordingly
}
else
{
// perform the code when button 2 is clicked
}
}

关于ios - 如何更改多个按钮图像和条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825025/

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