简体   繁体   English

如何检查是否没有点击任何按钮,那么它应该显示警报

[英]how to check if any of the button is not clicked then it should show alert

I have three question with radio button i want that user may give answer of all theree withu that it should not move to next screen so i have declared a BOOL isClicked and make it true when all the buttons are clicked. 我有三个问题与单选按钮,我希望用户可以给出所有theree的答案,它不应该移动到下一个屏幕,所以我声明了BOOL isClicked并在点击所有按钮时使其成立。

but it works when any of the buton is selected and it moves to next screen but it should check all three question button so i need to add differen bools for all the button or any thing else now i am doing like this 但它适用于任何一个按钮被选中并且它移动到下一个屏幕但它应该检查所有三个问题按钮所以我需要为所有按钮添加不同的bool或现在我正在做的任何其他事情这样

     -(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];



}

in this scenario you may add three different boolean values or instead you may check this by adding a counter. 在这种情况下,您可以添加三个不同的布尔值,或者您可以通过添加计数器来检查这一点。 Increment counter when you get answer. 当你得到答案时递增计数器。 and then you can replace your current if statement by if(noOfclicks > MIN_ANSWERS_REQUIRED) where noOfClicks will be your counter and MIN_ANSWERS_REQUIRED will be 3 in your case. 然后你可以用if(noOfclicks > MIN_ANSWERS_REQUIRED)替换你当前的if语句,其中noOfClicks将是你的计数器,MIN_ANSWERS_REQUIRED将在你的情况下为3。

Or 要么

As you said these are answers, so you must be assigning value for it in some variable. 如你所说这些是答案,所以你必须在某个变量中为它赋值。 You can assign this variable a default value which is not from the answers. 您可以为此变量指定一个默认值,该值不是来自答案。 Then when user clicks for navigating to next screen, check if all answers have different value than default value. 然后,当用户单击导航到下一个屏幕时,检查所有答案是否具有与默认值不同的值。

in .h file declare int count; 在.h文件中声明int count; in Viewdidload make count = 0; 在Viewdidload中make count = 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];
}
}

maybe it will help you. 也许它会帮助你。 Happy Coding 快乐的编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM