关于xcode:UIAlertview被多次调用

UIAlertview is called multiple times

我的应用程序中包含以下代码。在视图控制器上,我有两个UIButton控件,每个控件都执行不同的操作。当我按下第一个按钮时,我有一个UIAlertView来确认操作。这很好。我以相同的方式设置了第二个按钮。当我按下第二个按钮时,第一个UIAlertView短暂出现,然后出现第二个UIAlertView。那时它可以正常工作,但是第一个UIAlertView再次出现。

如果我完全取出UIAlertViews并只是更新视图上的标签以指示按下了哪个按钮,则不会再次调用任何一个按钮,因此我将其隔离为包含UIAlertViews。

任何人都可以指出导致此问题的代码中的某些内容吗?这是代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- (IBAction)clearInspectionsClicked {

    UIAlertView *alertClear = [[UIAlertView alloc] initWithTitle:@"Please Confirm"
                                            message:@"Clear out all inspection data?"
                                            delegate:self
                                            cancelButtonTitle:@"Clear"
                                            otherButtonTitles:@"Cancel", nil];
[alertClear show];

}

- (IBAction)loadSampleDataClicked {

    UIAlertView *alertLoad = [[UIAlertView alloc] initWithTitle:@"Please Confirm"
                                                    message:@"Load Sample data?"
                                                   delegate:self
                                          cancelButtonTitle:@"Load"
                                          otherButtonTitles:@"Cancel", nil];
    [alertLoad show];
}


-(void) alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if ([title isEqualToString:@"Clear"])
    {
        [self clearInspections];
        [self.StatusLabel setText:@"Inspection data has been cleared!"];
    }
    if ([title isEqualToString:@"Load"])
    {
        [self loadSampleData];
        [self.StatusLabel setText:@"Sample data has been loaded!"];
    }
}

您是否可能将其中一个按钮连接到其中两个操作?可以将多个动作关联到Interface Builder中的一个给定控件,这将导致这种确切的行为。