2012-01-12 81 views
0

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

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

任何人都可以指向我的代码中导致这种情况的东西?这是代码。

- (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!"]; 
    } 
} 

回答

1

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

+0

如果我取出UIAlertViews,而只是在视图的标签中显示状态消息,则只会调用一个动作。不过,我会通过调试器检查确认。感谢帖子! – 2012-01-14 13:22:04

+0

就是这样!感谢您指出了这一点。看完这些动作之后,我注意到第二个按钮已连接到两者。我删除了错误的操作链接,现在它工作正常。有时候解决方案比你想象的要简单!再次感谢! – 2012-01-14 13:36:47