2011-01-11 59 views
1

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

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

    } 
+0

如果你想使用应用-Mail,您必须使用MessageUI.framework,例如以模式呈现MFMailComposeViewController – Felix 2011-01-11 20:20:22

+0

我不想使用应用内邮件...我想将用户发送到mail.app – Krismutt 2011-01-11 20:22:32

+0

对不起。我意识到我的错误。谢谢! – Krismutt 2011-01-11 20:34:05

回答

5

你需要设置你的UIAlertView对象tag并在委托他们进行切换方法,这就是为什么委托方法需要在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... 
    } 
} 
2

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

  1. 使用2种不同的代表和指定clickedButtonAtIndex - 方法为每个类。
  2. clickedButtonAtIndex - 方法首先检查哪个alertview已发送消息。这需要标记UIAlertView(请参阅Jacob Relkin的答案)或为每个UIAlertView创建一个实例变量。
0

你应该指定你的哪个按钮是取消按钮,然后你需要检查哪个按钮被点击,如果是取消按钮,不要做任何事情。即,当您创建警报:

alertView.cancelButtonIndex = 1; 

当你得到的按钮点击消息:

if (buttonIndex == alertView.cancelButtonIndex) return;