Как проверить, не нажата ли какая-либо кнопка, тогда она должна показывать предупреждение
У меня три вопроса с переключателем, и я хочу, чтобы пользователь мог ответить на все три вопроса, если он не переместится на следующий экран, поэтому я объявил BOOL isClicked и сделаю его истинным, когда нажаты все кнопки.
но он работает, когда выбрана какая-либо из кнопок, и переходит к следующему экрану, но он должен проверить все три кнопки вопроса, поэтому мне нужно добавить разные bools для всех кнопок или что-то еще, сейчас я делаю вот так
-(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];
}
2 ответа
В этом сценарии вы можете добавить три различных логических значения или вместо этого вы можете проверить это, добавив счетчик. Счетчик приращений, когда вы получите ответ. а затем вы можете заменить текущее заявление if на if(noOfclicks > MIN_ANSWERS_REQUIRED)
где noOfClicks будет вашим счетчиком, а MIN_ANSWERS_REQUIRED будет 3 в вашем случае.
Или же
Как вы сказали, это ответы, поэтому вы должны присваивать ему значение в некоторой переменной. Вы можете присвоить этой переменной значение по умолчанию, которого нет в ответах. Затем, когда пользователь нажимает для перехода к следующему экрану, проверьте, все ли ответы имеют значение, отличное от значения по умолчанию.
В.h файле объявить int count; в 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];
}
}
Может быть, это поможет вам. Счастливое Кодирование