gpt4 book ai didi

ios - 选择和取消选择时设置按钮图像

转载 作者:行者123 更新时间:2023-11-28 21:41:27 26 4
gpt4 key购买 nike

我只是想在我的 IBAction 中设置按钮图像以切换到不同的图像:

if ([sender isSelected]) {
//1
UIImage *unselectedImage = [[UIImage alloc]initWithContentsOfFile:@"icon_checkbox_up"];
[sender setImage:unselectedImage forState:UIControlStateSelected];
[sender setSelected:NO];
}
else {
//2
UIImage *unselectedImage = [[UIImage alloc]initWithContentsOfFile:@"icon_checkbox_down"];
[sender setImage:unselectedImage forState:UIControlStateNormal];
[sender setSelected:YES];
}

当我点击按钮时,图像相应改变(在评论 2 下),再次按下按钮后,图像就消失了。我不确定为什么会这样。我已经尝试了 UIControlStateSelected 和 UIControlStateNormal。我错过了什么吗?

最佳答案

如果你的文件名没有错,你可以试试这个方法。希望对您有所帮助!!

[buttn addTarget:self action:@selector(Selected) forControlEvents:UIControlEventTouchDown];
[buttn addTarget:self action:@selector(Unselected) forControlEvents:UIControlEventTouchUpInside];
[buttn addTarget:self action:@selector(Unselected) forControlEvents:UIControlEventTouchUpOutside];

-(void)Selected
{
UIImage *unselectedImage = [[UIImage alloc]initWithContentsOfFile:@"icon_checkbox_up"];
[buttn setImage:unselectedImage forState:UIControlStateSelected];
[buttn setSelected:NO];
}

-(void)Unselected
{
UIImage *unselectedImage = [[UIImage alloc]initWithContentsOfFile:@"icon_checkbox_down"];
[buttn setImage:unselectedImage forState:UIControlStateNormal];
[buttn setSelected:YES];
}

关于ios - 选择和取消选择时设置按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877480/

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