user440096
user440096

Reputation:

Handling alert buttons when your view has more than 1 alert

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

As you can see above, I have 2 alerts. But they both call the same method to handle there key presses. How can I tell which alert is currently alive and respond to the keypresses differently depending on which alert is up?

Upvotes: 1

Views: 349

Answers (1)

Thomas Clayson
Thomas Clayson

Reputation: 29935

Use: [alert setTag:1]; and [alert setTag:2]; respectively

then you can do:

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

Upvotes: 2

Related Questions