当您的视图有超过1个警报时处理警报按钮

时间:2010-09-24 10:50:46

标签: iphone objective-c

-(void)alertOKCancelAction 
{
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Return Home" message:@"Are 
    you sure you want to return to the menu?" delegate:self cancelButtonTitle:@"Cancel"  
    otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}

-(void)alertConnectionLost 
{
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Lost"
    message:@"The connection to the other device has been lost" delegate:self  
    cancelButtonTitle:nil otherButtonTitles:@"OK", nil];    
[alert show];
[alert release];
}


- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{

}
else
{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}
}

如上所示,我有2个提醒。但他们都用相同的方法来处理按键。如何判断哪个警报当前处于活动状态并根据哪个警报响应而以不同方式响应按键?

1 个答案:

答案 0 :(得分:2)

分别使用:[alert setTag:1];[alert setTag:2];

然后你可以这样做:

if([actionSheet tag] == 1){
  //do thing for first alert view
}
else if([actionSheet tag] == 2){
  //do something for second alert view
}