1

基本上,我尝试做的是添加多个确认警报......但我无法让它工作。无论我按什么确认警报,“继续”按钮都会导致完全相同的事情(没有文字的正文和带有“XXXX”的主题)......知道如何使确认警报导致不同的事情吗?

编辑2;无论我按什么按钮(继续或关闭),应用程序都会将用户发送到 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:@"....."];
                        [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) {
        }

    }
4

3 回答 3

5

您需要tag在您的UIAlertView对象上设置 a 并在您的委托方法中打开它们,这就是委托方法接受 的原因UIAlertView,因此您可以根据按下按钮的对象来执行操作。

#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...
  }
}
于 2011-01-11T17:37:40.757 回答
2

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.
于 2011-01-11T17:40:15.440 回答
0

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;
于 2011-01-11T20:31:53.197 回答