-1

我有一个奇怪的问题radio button。我使用UIButtonradio button使用了两个图像btnCheck.pngbtnUnCheck.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,如果没有选择任何一项,那么它会显示消息。但问题是当我选择这两个项目并进入后台时,我会在应用程序中返回并单击下一步,然后它会显示消息,但复选框已被选中。为什么在那种情况下条件不正确......

在此处输入图像描述

4

6 回答 6

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值,它会正常工作。

于 2013-06-21T05:12:23.273 回答
0

您不应该通过按钮的设置图像进行比较。例如,使用按钮的内置selected属性。

btnTapeIn.selected = YES;
...
if(btnTapeIn.isSelected)
...
于 2013-06-21T05:16:04.593 回答
0

您可以将图像设置为正常状态并选择XIB并更改状态IBAction

于 2013-06-21T11:14:52.743 回答
0

单击该按钮进行检查时,使用 Buttons selected 属性而不是图像

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;
}
于 2013-06-21T05:10:38.737 回答
0
https://github.com/Brayden/UICheckbox      

您可以实现这一点并减少比较逻辑。

于 2013-06-21T05:13:16.627 回答
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
于 2013-06-21T05:23:52.953 回答