在进入下一个视图中需要确保当前的控制段都被选中了
-(void)checkAllSegments
{ BOOL alertShown;
alertShown = NO;
for (UISegmentedControl *swItem in allSegmentControlOutlet) {
int selectedSegment = swItem.selectedSegmentIndex;
swItem.segmentedControlStyle = UISegmentedControlStyleBar;
//didPass = YES;
if (selectedSegment == -1) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fill in all forms"
message:@"Please go back and fill in missing info"
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
// [swItem setTintColor:[UIColor redColor]];
if (!alertShown) {
[alert show];
alertShown = YES;
didPass = NO;
return;
}
}
}
if (didPass) {
[self performSegueWithIdentifier:@"ToHiring" sender:self];
}
}
但是我不知道应该将 didPass = YES;
放到哪,这句代码所在的位置会被注释掉。除非最后一条循环完成。有没有更好的方法解决?
给你改一下。
-(void)checkAllSegments
{ BOOL alertShown;
alertShown = NO;
for (UISegmentedControl *swItem in allSegmentControlOutlet) {
int selectedSegment = swItem.selectedSegmentIndex;
swItem.segmentedControlStyle = UISegmentedControlStyleBar;
//didPass = YES;
if (selectedSegment == -1) {
alertShown=YES;
break; /////如果有一个没有被选择,直接退出for循环
}
}
if (alertShown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fill in all forms"
message:@"Please go back and fill in missing info"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
}else {
[self performSegueWithIdentifier:@"ToHiring" sender:self];
}
}