“If”语句的困惑(原来是 UIAlertView 的困惑)

Posted

技术标签:

【中文标题】“If”语句的困惑(原来是 UIAlertView 的困惑)【英文标题】:"If" statement quandary (turned out to be a UIAlertView quandary) 【发布时间】:2014-05-08 05:29:40 【问题描述】:

我已经与这个代码搏斗了一天多,所以终于寻求帮助。

我相信,我的问题在于这两种方法之一,其目的是检测需要显示警报的情况。第一种方法,由“完成”按钮激活,从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,我尝试将其删除,并控制“通过”警报到后续代码。我需要对此做一些进一步的研究,但我非常感谢您的指导!

非常感谢!

【问题讨论】:

您需要使用 alertview 委托才能根据用户在 alertview 窗口中的选择来执行代码。看***.com/a/4248387/986169 我隐约知道 UIAlertViewDelegate,但在我的应用程序中一直在使用警报,而不使用委托。这是我遇到的第一个问题(据我所知)。我需要一些时间来研究这个并相应地更改代码。非常感谢您的快速回复,一旦我实施了您的建议,我会回来更新! 请参阅上面的更新。如果您想将您的回复放入答案中,我很乐意接受并投票。谢谢! 【参考方案1】:

我还不明白你的整个代码,我想这就是你想要实现的。

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


【讨论】:

以上是关于“If”语句的困惑(原来是 UIAlertView 的困惑)的主要内容,如果未能解决你的问题,请参考以下文章

iOS6 UIAlertView.title 坏了?

结束语句之困惑

(Objective-)C 中的 If 语句

Xcode:带有文本框的 If 语句

如何暂停语句执行和 UIalertview 在单独的循环上运行

我可以移动 UIAlertView 吗?