添加多个确认警报 (UIAlertView)

发布于 2024-10-11 19:07:31 字数 2395 浏览 12 评论 0原文

基本上,我尝试做的是添加多个确认警报......但我就是无法让它工作。无论我按什么确认警报,“继续”按钮都会导致完全相同的结果(没有文本的正文和带有“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) {
        }

    }

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:@"....."];
                        [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) {
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2024-10-18 19:07:31

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

#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...
  }
}

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...
  }
}
仙气飘飘 2024-10-18 19:07:31

委托方法由 UIAlertViewDelegate 协议指定,您无法更改它。
您可以做两件事:

  1. 使用 2 个不同的委托并为每个类指定一个 clickedButtonAtIndex 方法。
  2. clickedButtonAtIndex 方法中,首先检查哪个警报视图发送了消息。这需要标记 UIAlertView (请参阅 Jacob Relkin 的回答)或为每个 UIAlertView 创建一个实例变量。

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.
奶茶白久 2024-10-18 19:07:31

您应该指定哪个按钮是取消按钮,然后您需要检查单击了哪个按钮,如果是取消按钮,则不执行任何操作。即,当您创建警报时:

alertView.cancelButtonIndex = 1;

当您收到单击按钮的消息时:

if (buttonIndex == alertView.cancelButtonIndex) return;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文