Peter Schultz
Peter Schultz

Reputation: 219

Multiple AlertView with tags, can't cancel

I searched Stack for ways to implement multiple alertViews. Most of the answers were to use Tags. This way works great, except for one huge thing- the cancel button! When the alertView pops up, whether you tap "cancel" or "yourButtonTitle", your action goes through. Is there a way to cancel an alertView with Tags?

Here is my code:

#define TAG_ONE 1
#define TAG_TWO 2

- (IBAction)someButton1 {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Call" message:@"Call number?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil];

alertView.tag = TAG_ONE;
[alertView show];
}

- (IBAction)someButton2 {

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Log Out?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];

alertView.tag = TAG_TWO;
[alertView show];
}

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

if (alertView.tag == TAG_ONE) { 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:101-101-1010"]];

} else if (alertView.tag == TAG_TWO){ 
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}
}

Upvotes: 0

Views: 39

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

you can set TAG for only the UIAlertview , but you can Identify the button using button Index

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

if (alertView.tag == TAG_ONE) {
if(alertView.cancelButtonIndex == buttonIndex){
// Do cancel
}
else{
// Do the success thing
}
}
else if (alertView.tag == TAG_TWO) {
// same thing followed
}
}

buttonIndex == 0 // for OK , buttonIndex == 1 // for Cancel

additional Reference

Upvotes: 1

Peter Schultz
Peter Schultz

Reputation: 219

You must add if (alertView.cancelButtonIndex == buttonIndex){ // Do cancel } before asking if (alertView.tag == TAG_ONE) {

Upvotes: 0

Related Questions