views:

31

answers:

1

I have two UIAlertView's which are not displayed one after another. Both of the have two buttons and I need to determine which button was pressed. I've tried to use

- (void)alertOKCancelAction {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title"
                                               message:@"Message" delegate:self
                                     cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alert show];
[alert release];
}  

 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{    //Code

}
else
{//Code

}
}

But this code doesn't work if I have two UIAlertViews.

Can you help me? Thanks in advance!

+1  A: 

Looks like you might be able to optimize your design a little bit. Why not wrap a method around your UIAlertView, then pass in the information you need to display the alert.

Then use

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
      NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
      //....Do something based on the btnTitle that was clicked.

}

To check which button was clicked based on the title.

Jordan
Thank you! It works and also optimizes the code!
Knodel