开发者

Seperate event handlers for different UIAlerts in objective c

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

fine,,say i have 2 uialerts and delegate set to s开发者_如何转开发elf in both the cases and first uialert contains (ok & download)buttons second contains (cancel & upload)buttons now we need separate event handlers know?


To handle multiple UIAlertView within a UIView, you have to set unique tag for each.

    alert.tag = 123;

And while get response from delegate method manage each with unique tag.

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


Try setting tag property for two different UIAlertView instances and then check back those tags again in the callback and do the rest there, example:

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];

And here is the 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
     }
}


               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");
       }
  }


If you have a lot of alert views or the delegate methods are complex then you could create a controllers (an simple NSObject subclass) to manage each alert view, e.g. DownloadConfirmationAlertController. Your main controller can then store a reference to the sub-controllers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜