我已经和这段代码搏斗了一天多,所以我终于要求帮助了。
我认为,我的问题在于这两种方法中的一种,其目的是检测需要显示警报的条件。第一种方法,由" Done"按钮,查找UITableview
的重复用户选择:
- (IBAction)doneButton:(UIBarButtonItem *)sender
{
if ([self.firstActivityLabel.text isEqualToString: @"1st"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity selected"
message:@"Please select an activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else if ([self.secondActivityLabel.text isEqualToString: @"2nd"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need two activities for graph"
message:@"Please select another activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
if (self.currentSpec.fromDate)
{
// Assign activityOfInterest and benchmarkActivity
// fromDate and toDate already assigned in timeFrameSelector if a time frame has been selected
self.currentSpec.activityOfInterest = self.firstActivityLabel.text;
self.currentSpec.benchmarkActivity = self.secondActivityLabel.text;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No time frame selected"
message:@"Please select a time frame or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
[self checkForInstances];
// Sends to delegate AFTER all other decisions have been made and handled
[self.delegate AvsAViewControllerIsDone:self.currentSpec];
// Set timeFrameButton index so nothing is highlighted
self.timeFrameButton.selectedSegmentIndex = -1;
}
如果这些条件在每种情况下都能正确解析,则第二种方法起作用,检查以确保持久存储中存在所选项的实例并警告各种条件:
-(void) checkForInstances
{
NSPredicate *aOiCountFinderPredicate = [NSPredicate predicateWithFormat:@"name == %@",self.currentSpec.activityOfInterest];
int aOiCount = [TimedActivity MR_countOfEntitiesWithPredicate:aOiCountFinderPredicate];
NSPredicate *bmaCountFinderPredicate = [NSPredicate predicateWithFormat:@"name == %@",self.currentSpec.benchmarkActivity];
int bmaCount = [TimedActivity MR_countOfEntitiesWithPredicate:bmaCountFinderPredicate];
// Alerts if no instances of selected item
NSString *firstAlertstr=[NSString stringWithFormat:@"No instances of %@",self.currentSpec.activityOfInterest];
NSString *secondAlertstr=[NSString stringWithFormat:@"No instances of %@",self.currentSpec.benchmarkActivity];
NSString *thirdAlertstr=[NSString stringWithFormat:@"No instances of either selected criteria"];
if (aOiCount == 0 && bmaCount > 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:firstAlertstr message:@"Please clear and select a different comparison" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[self.myTableView setUserInteractionEnabled:NO];
}
else if (bmaCount == 0 && aOiCount > 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:secondAlertstr message:@"Please clear and select a different comparison" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[self.myTableView setUserInteractionEnabled:NO];
[alert show];
}
else if (aOiCount == 0 && bmaCount == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:thirdAlertstr message:@"Please clear and select a different comparison or timeframe" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[self.myTableView setUserInteractionEnabled:NO];
}
NSLog(@"Number of instances of %@ is %d",self.currentSpec.activityOfInterest,aOiCount);
NSLog(@"Number of instances of %@ is %d",self.currentSpec.benchmarkActivity,bmaCount);
}
在我们进入第二种方法之前,事情似乎正常。它正确地抛出规定的警报,但是直到控制返回到第一个方法,然后到委托方法([self.delegate AvsAViewControllerIsDone:self.currentSpec];
),即委托方法在警报出现之前触发。但当然,警报旨在阻止事情发生,以便用户在进入该方法之前可以重新考虑并纠正他的选择。
我确定感谢有人指出我的错误。我希望它不是真正愚蠢的东西,但我不会指望它。
感谢您的期待!
更新
根据@giorasch的建议,我修改了我的代码:
- (IBAction)doneButton:(UIBarButtonItem *)sender
{
if (self.currentSpec.fromDate)
{
// Assign activityOfInterest and benchmarkActivity
// fromDate and toDate already assigned in timeFrameSelector if a time frame has been selected
self.currentSpec.activityOfInterest = self.firstActivityLabel.text;
self.currentSpec.benchmarkActivity = self.secondActivityLabel.text;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No time frame selected"
message:@"Please select a time frame or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
[self checkForInstances];
}
-(void) checkForInstances
{
NSPredicate *aOiCountFinderPredicate = [NSPredicate predicateWithFormat:@"name == %@",self.currentSpec.activityOfInterest];
int aOiCount = [TimedActivity MR_countOfEntitiesWithPredicate:aOiCountFinderPredicate];
NSPredicate *bmaCountFinderPredicate = [NSPredicate predicateWithFormat:@"name == %@",self.currentSpec.benchmarkActivity];
int bmaCount = [TimedActivity MR_countOfEntitiesWithPredicate:bmaCountFinderPredicate];
// Alerts if no instances of selected item
NSString *firstAlertstr=[NSString stringWithFormat:@"No instances of %@",self.currentSpec.activityOfInterest];
NSString *secondAlertstr=[NSString stringWithFormat:@"No instances of %@",self.currentSpec.benchmarkActivity];
NSString *thirdAlertstr=[NSString stringWithFormat:@"No instances of either selected criteria"];
if (aOiCount == 0 && bmaCount > 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:firstAlertstr message:@"Please clear and select a different comparison or timeframe" delegate:self cancelButtonTitle:@"Clear" otherButtonTitles:nil];
[alert setTag:1];
// checksPositiveForInstances = NO;
// [self alertView:alert didDismissWithButtonIndex:0];
[alert show];
}
else if (bmaCount == 0 && aOiCount > 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:secondAlertstr message:@"Please clear and select a different comparison or timeframe" delegate:self cancelButtonTitle:@"Clear" otherButtonTitles:nil];
[alert setTag:2];
// checksPositiveForInstances = NO;
// [self alertView:alert didDismissWithButtonIndex:0];
[alert show];
}
else if (aOiCount == 0 && bmaCount == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:thirdAlertstr message:@"Please clear and select a different comparison or timeframe" delegate:self cancelButtonTitle:@"Clear" otherButtonTitles:nil];
[alert setTag:3];
// checksPositiveForInstances = NO;
// [self alertView:alert didDismissWithButtonIndex:0];
[alert show];
}
else // Instances present in both registers, no alert, sends to delegate for processing
{
[self.delegate AvsAViewControllerIsDone:self.currentSpec];
}
NSLog(@"Number of instances of %@ is %d",self.currentSpec.activityOfInterest,aOiCount);
NSLog(@"Number of instances of %@ is %d",self.currentSpec.benchmarkActivity,bmaCount);
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
[self clearEverything]; // Responds to Clear button (cancelButton) on alert
}
}
-(void) clearEverything
{
UIButton *clearButton = (UIButton *)[self.view viewWithTag:102];
// UIButton *selectButton = (UIButton *)[self.view viewWithTag:101];
[self.mainSelectLabel setText:@"Select first activity"];
[self.timeFrameButton setHidden:YES];
[self.timeFrameLabel setHidden:YES];
// Set timeFrameButton index so nothing is highlighted
self.timeFrameButton.selectedSegmentIndex = -1;
for (NSInteger j = 0; j < [self.myTableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [self.myTableView numberOfRowsInSection:j]; ++i)
{
UITableViewCell * cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.myTableView deselectRowAtIndexPath:self.lastIndexPath animated:NO];
}
}
self.lastIndexPath = nil;
self.firstActivityLabel.text = @"1st";
self.secondActivityLabel.text = @"2nd";
[self.myTableView setUserInteractionEnabled:YES];
clearButton.userInteractionEnabled = NO;
UIBarButtonItem *doneButton = self.navigationItem.rightBarButtonItem;
doneButton.enabled = NO;
}
现在一切正常,但老实说,我并不完全清楚在这种特殊情况下代表是必要的。 OTOH,我尝试将其删除,并控制&#34;掉线&#34;对后续代码的警报。我需要对此进行进一步的研究,但我非常感谢指导!
非常感谢!
答案 0 :(得分:0)
我还没有理解你的整个代码,我认为这是你想要实现的目标。
- (IBAction)doneButton:(UIBarButtonItem *)sender
{
if ([self.firstActivityLabel.text isEqualToString: @"1st"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity selected"
message:@"Please select an activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else if ([self.secondActivityLabel.text isEqualToString: @"2nd"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Need two activities for graph"
message:@"Please select another activity or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else{
if (!self.currentSpec.fromDate)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No time frame selected"
message:@"Please select a time frame or Cancel"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
// Assign activityOfInterest and benchmarkActivity
// fromDate and toDate already assigned in timeFrameSelector if a time frame has been selected
self.currentSpec.activityOfInterest = self.firstActivityLabel.text;
self.currentSpec.benchmarkActivity = self.secondActivityLabel.text;
[self checkForInstances];
// Sends to delegate AFTER all other decisions have been made and handled
[self.delegate AvsAViewControllerIsDone:self.currentSpec];
// Set timeFrameButton index so nothing is highlighted
self.timeFrameButton.selectedSegmentIndex = -1;
}
}
}