views:

911

answers:

1

i have two Alertview in same viewcontroller and i want to use the delegate method

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex

this method will get called when button in alertview been pressed however both alertview will call the same method

how can i different the two alertView?

+1  A: 

Set the tag property to different values when you display the alert. It's just an integer and can be queried in the callback/delegate method.

Here's an example (using an ActionSheet rather than an AlertView, but the principle is exactly the same):

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Some option", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
actionSheet.tag = 10;
[actionSheet showInView:self.view];
[actionSheet release];

Then in your selector:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  switch (actionSheet.tag) {
    case 10:
      // do stuff
      break;
    case 20:
      // do other stuff
      break;
  }
}

Of course, you'd use constants rather than literal values, localised strings, etc, but that's the basic idea.

Stephen Darlington
i understand ur meaning it jus like set the button tag value,but i dont see alert view have a property is tag, can please show the code in details
tag is a property of UIView, of which UIAlertView (and, for that matter, UIButton) is a subclass, so it does have it.
executor21