添加多个确认图(UIAlertView)

时间:2011-01-11 17:29:32

标签: iphone ios4 uialertview alert confirmation

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

编辑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) {
        }

    }

3 个答案:

答案 0 :(得分:5)

您需要在tag个对象上设置UIAlertView并在委托方法中启用它们,这就是委托方法接收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...
  }
}

答案 1 :(得分:2)

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

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

答案 2 :(得分:0)

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

alertView.cancelButtonIndex = 1;

当你点击按钮时点击消息:

if (buttonIndex == alertView.cancelButtonIndex) return;