1

我有三个关于单选按钮的问题,我希望用户可以回答所有不应该移动到下一个屏幕的问题,所以我已经声明了一个 BOOL isClicked 并在所有按钮都被点击时使它成为真的。

但是当任何一个按钮被选中并且它移动到下一个屏幕时它会起作用但它应该检查所有三个问题按钮所以我需要为所有按钮或其他任何事情添加不同的布尔值现在我正在这样做

     -(IBAction)button1Action{


UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img forState:UIControlStateNormal];
[button2 setImage:img1 forState:UIControlStateNormal];
[button3 setImage:img1 forState:UIControlStateNormal];
    option1=@"Less than 2 hours";

isClicked = YES;
}
   -(IBAction)button2Action{


UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img1 forState:UIControlStateNormal];
[button2 setImage:img forState:UIControlStateNormal];
[button3 setImage:img1 forState:UIControlStateNormal];



option2=@"2-5 hours";

isClicked = YES;

}


   -(IBAction)button3Action{

UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img1 forState:UIControlStateNormal];
[button2 setImage:img1 forState:UIControlStateNormal];
[button3 setImage:img forState:UIControlStateNormal];
option3=@"More than 5 hours";

isClicked = YES;

}



   if (isClicked) {


    [self save_Local];


    SecondViewController*targetController=[[SecondViewController alloc]init];

    targetController.responseOne=responseOne;
    targetController.responseTwo=responseTwo;
    targetController.responseThree=responseThree;
    targetController.responseFour=responseFour;
    targetController.teritory=teritory;


    [self.navigationController pushViewController:targetController animated:YES];



}

else {





    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please provide all responses before proceeding to the next screen" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];



}
4

2 回答 2

2

在这种情况下,您可以添加三个不同的布尔值,或者您可以通过添加一个计数器来检查它。当你得到答案时增加计数器。然后您可以将当前的 if 语句替换为if(noOfclicks > MIN_ANSWERS_REQUIRED)noOfClicks 将是您的计数器,而 MIN_ANSWERS_REQUIRED 在您的情况下将是 3。

或者

正如您所说,这些是答案,因此您必须在某个变量中为其赋值。您可以为该变量分配一个不是来自答案的默认值。然后,当用户单击导航到下一个屏幕时,检查所有答案的值是否与默认值不同。

于 2013-01-31T05:48:13.460 回答
1

在 .h 文件中声明 int 计数;在 Viewdidload 中使计数 = 0;

-(IBAction)button1Action{
UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img forState:UIControlStateNormal];
[button2 setImage:img1 forState:UIControlStateNormal];
[button3 setImage:img1 forState:UIControlStateNormal];
option1=@"Less than 2 hours";
count=count+1;
}
-(IBAction)button2Action
{
UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img1 forState:UIControlStateNormal];
[button2 setImage:img forState:UIControlStateNormal];
[button3 setImage:img1 forState:UIControlStateNormal];
option2=@"2-5 hours";
count=count+1;
}
-(IBAction)button3Action{ 
UIImage *img = [UIImage imageNamed:@"selected.png"];
UIImage *img1=[UIImage imageNamed:@"unselected.png"];
[button1 setImage:img1 forState:UIControlStateNormal];
[button2 setImage:img1 forState:UIControlStateNormal];
[button3 setImage:img forState:UIControlStateNormal];
option3=@"More than 5 hours";
count=count+1;
}
-(IBAction)Push
{
if (count==3) {
    [self save_Local];
    SecondViewController*targetController=[[SecondViewController alloc]init];
    targetController.responseOne=responseOne;
    targetController.responseTwo=responseTwo;
    targetController.responseThree=responseThree;
    targetController.responseFour=responseFour;
    targetController.teritory=teritory;
    [self.navigationController pushViewController:targetController animated:YES];
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please provide all responses before proceeding to the next screen" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}

也许它会帮助你。快乐编码

于 2013-01-31T06:29:04.473 回答