在進入下一個視圖中需要確保當前的控制段都被選中了
-(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];
}
}