在目标c中为不同的UIAlert分离事件处理程序

时间:2011-09-13 06:25:21

标签: iphone objective-c xcode

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
    [alert show];
    [alert release];

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
            //Code for OK button
        }
        if (buttonIndex == 1)
        {
            //Code for download button
        }
    }

罚款,说我有两个uialerts和委托设置为自我在两个案件和第一uialert包含(确定和下载)按钮第二个包含(取消和上传)按钮现在我们需要单独的事件处理程序知道?

4 个答案:

答案 0 :(得分:12)

要在UIView中处理多个UIAlertView,您必须为每个UIA设置唯一标记。

    alert.tag = 123;

虽然来自委托方法的响应管理每个都有唯一标记。

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(alertView.tag == 123)
        {
            if (buttonIndex == 0)
            {
                //Code for OK button
            }
            else if (buttonIndex == 1)
            {
                //Code for download button
            }
       }
       else if(alertView.tag == 456)
       {
            // code to manage another alertview response.
       }
    }

答案 1 :(得分:4)

尝试为两个不同的tag实例设置UIAlertView属性,然后在回调中再次检查这些标记,并在那里完成其余的操作,例如:

UIAlertView *alertDownload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"download"];
    alertDownload.tag = 1;
    [alertDownload show];
    [alertDownload release];


UIAlertView *alertUpload = [[UIAlertView alloc]initWithTitle:@"title" message:@"szMsg" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"upload"];
    alertUpload.tag = 2;
    [alertUpload show];
    [alertUpload release];

这是Delegate CallBack,

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
     if(alertView.tag == 1) {
          //Here you do your stuff for Download
     }
     if(alertView.tag == 2) {
         //Here you do stuff for Upload
     }
}

答案 2 :(得分:1)

               my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self
                cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
             my_Alert.frame = CGRectMake(462, 359, 400, 50);

             my_Alert.tag = 1;

               my_Alert = [[UIAlertView alloc]initWithTitle:@"Hi" message:@"Hello" delegate:self
                cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
             my_Alert.frame = CGRectMake(462, 359, 400, 50);

                              my_Alert.tag = 2;

         - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {

   if (buttonIndex==0 && my_Alert.tag == 1)
    {   

      NSLog(@"Perform action on button touch of index 0 of First Alert");
    }
      else
       {
             NSLog(@"Perform action on button touch of index 1 of First Alert");
       }

    if (buttonIndex==0 && my_Alert.tag == 2)
   {  
   NSLog(@"Perform action on button touch of index 0 of Second Alert");         
   }
           else
       {
             NSLog(@"Perform action on button touch of index 1 of Second Alert");
       }
  }

答案 3 :(得分:0)

如果您有很多警报视图或委托方法很复杂,那么您可以创建一个控制器(一个简单的NSObject子类)来管理每个警报视图,例如: DownloadConfirmationAlertController。然后,您的主控制器可以存储对子控制器的引用。