开发者

Adding more than one confirmationalerts (UIAlertView)

Basically, what I try to do is to add multiple confirmationalerts...but I just cant get it to work. No matter what confirmationalert i press, the "Continue" button leads to the exact same thing (a body without text and a subject with "XXXX")... Any idea how to make the confimationalerts to lead to different things?

EDIT 2; No matter what button I press (continue or dismiss), the app sends the user to mail.app...

        -(IBAction)mail {

                    UIAlertView *mail = [[UIAlertView alloc] init];
                        [mail setTag:ALERTVIEW_MAIL_TAG];
                        [mail setTitle:@"Open mail"];
                        [mail setMessage:@"....."];
                        [mail setDelegate:self];
                        [mail addButtonWithTitle:@"Continue"];
                        [mail addButtonWithTitle:@"Dismiss"];
                        [mail show];
                        [mail release];

                    }

                    -(IBAction)feedback {
                        UIAlertView *feedback = [[UIAlertView alloc] init];
                        [feedback setTag:ALERTVIEW_TIPSA_TAG];
                        [feedback setTitle:@"Open mail"];
                        [feedback setMessage:@"....."];
          开发者_Go百科              [feedback setDelegate:self];
                        [feedback addButtonWithTitle:@"Continue"];
                        [feedback addButtonWithTitle:@"dismiss"];
                        [feedback show];
                        [feedback release];
                    }

- (void)showConfirmAlert
                    {   
                    }   

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
                   if([alertView tag] == ALERTVIEW_FEEDBACK_TAG) {
                        NSURL *url = [[NSURL alloc] initWithString:@"mailto:?subject=XXXX"];
                        [[UIApplication sharedApplication] openURL:url];
                        [url release];
                    }

        else if (buttonIndex == 1) {
        }



           else  if ([alertView tag] == ALERTVIEW_MAIL_TAG) {
                        NSString *subject = @"YYYY";
                        NSString *body = @".....";
                        NSString *path = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@", subject, body];
                        NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                        [[UIApplication sharedApplication] openURL:url];
                    }

            else if (buttonIndex == 1) {
        }

    }


You'll need to set a tag on your UIAlertView objects and switch on them in your delegate method, this is why the delegate method takes in the UIAlertView, so you can do stuff based on which object the button was pressed.

#define ALERTVIEW_MAIL_TAG     100
#define ALERTVIEW_FEEDBACK_TAG 101


- (IBAction) feedback {
   UIAlertView *feedback = [[UIAlertView alloc] init];
   [feedback setTag:ALERTVIEW_FEEDBACK_TAG];
   //...
}

- (IBAction) mail {
   UIAlertView *mail = [[UIAlertView alloc] init];
   [mail setTag:ALERTVIEW_MAIL_TAG];
}

-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
  if([alertView tag] == ALERTVIEW_MAIL_TAG) {
     //do stuff...
  } else {
     //do other stuff...
  }
}


The delegate method is specified by the UIAlertViewDelegate protocol, you can't change that. There are 2 things you can do:

  1. Use 2 different delegates and specify a clickedButtonAtIndex-method for each class.
  2. In the clickedButtonAtIndex-method first check which alertview has sended the message. This requires to tag the UIAlertView (see answer by Jacob Relkin) or to create an instance variable for each UIAlertView.


You should specify which of your buttons is the cancel button, and then you need to check which button was clicked and don't do anything if it was the cancel button. I.e., when you create the alert:

alertView.cancelButtonIndex = 1;

And when you get the button clicked message:

if (buttonIndex == alertView.cancelButtonIndex) return;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜