radio button
我有一个奇怪的问题。我将UIButton
用于radio button
并使用了两张图片btnCheck.png
和btnUnCheck.png
,正常情况下它正常工作没有问题。这是我点击按钮上的代码
-(IBAction)btnNextClick:(id)sender {
UIImage* selectedImg=[UIImage imageNamed:@"btnCheck.png"];
if ((btnILink.imageView.image == selectedImg || btnTapeIn.imageView.image == selectedImg || btnKeraLink.imageView.image == selectedImg))
{
if (btnTapeIn.imageView.image == selectedImg) {
UIImage* selectedImg1=[UIImage imageNamed:@"btnCheck.png"];
if (!(btn18.imageView.image == selectedImg1 || btn22.imageView.image == selectedImg1)){
[FunctionManager showMessage:@"" withMessage:@"Please Select Length" withDelegage:nil];
return;
}
}
else {
UIImage* selectedImg1=[UIImage imageNamed:@"btnCheck.png"];
if (!(btn16.imageView.image == selectedImg1 || btn18.imageView.image == selectedImg1 || btn22.imageView.image == selectedImg1)){
[FunctionManager showMessage:@"" withMessage:@"Please Select Length" withDelegage:nil];
return;
}
}
}
else {
[FunctionManager showMessage:@"" withMessage:@"Please Select Method" withDelegage:nil];
return;
}
}
当我点击btnNext
时它正常工作,如果没有选择任何一项,则显示消息。但问题是当我选择这两个项目并进入后台然后我在应用程序中回来然后单击下一步然后它显示消息,但是已经选中了复选框。为什么在这种情况下情况不正确......
答案 0 :(得分:3)
是的,它会这样做,因为您比较了UIImage
对象赞:
UIImage* selectedImg=[UIImage imageNamed:@"btnCheck.png"];
if ((btnILink.imageView.image == selectedImg || btnTapeIn.imageView.image == selectedImg || btnKeraLink.imageView.image == selectedImg))
每当它来自背景时,它设置创建新的UIImage
对象,因此它不能是相同的object
。
因此,不是比较对象(btnILink.imageView.image == selectedImg)
而是取bool
值,它将正常工作。
答案 1 :(得分:1)
单击该按钮进行检查时,使用按钮选择属性而不是图像 喜欢
if(btnTapeIn.selected)
{
[btnTapeIn setImage:[UIImage imageNamed:@"btnUnCheck.png"] forState:UIControlStateNormal];
btnTapIn.selected = NO;
}
else
{
[btnTapeIn setImage:[UIImage imageNamed:@"btnCheck.png"]forState:UIControlStateNormal];
btnTapIn.selected = YES;
}
答案 2 :(得分:0)
https://github.com/Brayden/UICheckbox
您可以实现此目的并减少比较逻辑。
答案 3 :(得分:0)
您不应该按按钮的设定图像进行比较。例如,使用内置selected
属性的按钮。
btnTapeIn.selected = YES;
...
if(btnTapeIn.isSelected)
...
答案 4 :(得分:0)
- (IBAction)btn_GenderCheck:(id)sender {
switch ([sender tag])
{
case 1: //-------for Male
[btn_malecheck setImage:[UIImage imageNamed:@"radio_select.png"] forState:UIControlStateNormal];
[btn_femalecheck setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateNormal];
isMaletag=1;
break;
case 2: //----------for Female
[btn_malecheck setImage:[UIImage imageNamed:@"radio.png"] forState:UIControlStateNormal];
[btn_femalecheck setImage:[UIImage imageNamed:@"radio_select.png"] forState:UIControlStateNormal];
isMaletag=2;
break;
default:
break;
}
}
// then you can check tag and accordingly alert your message
答案 5 :(得分:0)
您可以将图片设置为正常状态并在XIB
中选择并更改IBAction