views:

29

answers:

1
-(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?

+2  A: 

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
}
Thomas Clayson
The is the approach I use, only I go one step further and define the tag values as constants, so they can be named. Just makes it a little easier to understand after seeing the code for the first time.
Jasarien
not entirely sure what you mean, appologies. I thought a tag could only be of class `NSInteger`?
Thomas Clayson