Two Alert Views in One View Controller - buttonIndex Response

前端 未结 4 1140
日久生厌
日久生厌 2021-01-13 06:01

I am trying to perform the smile task of having two alerts in a single View Controller. The code below works fine, but how would I make another instance of it elsewhere in t

相关标签:
4条回答
  • 2021-01-13 06:21

    Simply set a tag to each Alert view and check which one sent the messeg.

    alertView.tag=0;
    

    And then

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(alertView.tag==0){
    
          if(buttonIndex == 0)//OK button pressed
         {
           //do something
         }
         else if(buttonIndex == 1)//Annul button pressed.
         {
          //do something
         }
    }else{
    
      if(buttonIndex == 0)//OK button pressed
    
           {
            //do something
          }
          else if(buttonIndex == 1)//Annul button pressed.
        {
        //do something
        }
      }
    
    0 讨论(0)
  • 2021-01-13 06:25

    set tag to alert view.

    alert.tag = 1;
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 1 && alertView.tag == 1) 
        {
        //do something
        }
    }
    
    0 讨论(0)
  • 2021-01-13 06:26

    You can use the tag property on UIAlertView to decipher which alert is which:

    -(void)alertChoice
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
        message:@"Message" delegate:self
        cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
        alert.tag = 0;
        [alert show];
    }
    
    -(void)alertChoice1
    {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title"
        message:@"Message" delegate:self
        cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
        alert1.tag = 1;
        [alert1 show];
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(alertView.tag == 0)
        {
        }
    }
    
    0 讨论(0)
  • 2021-01-13 06:32

    Step 1: Add UIAlertViewDelegate in your view controller.h file

    step 2: Add the following methods in your view controller.m file

    -(void)AlertMethodOne
    {
      UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"AlertMethodOne" message:@"AlertMethodOne successfully Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
        alert.tag=101;
        [alertview show];  
    }
    
    -(void)AletrMethodTwo
    {
    UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"AletrMethodTwo" message:@"AlertMethodTwo successfully Called" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    alert.tag=102;
        [alertview show]; 
    }
    

    call the above two methods in your viewController as like below: [self AlertMethodOne]; [self AlertMethodTwo];

    Now AlertView Button Clicked Method

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if(alertView.tag==101)
    {
       if (buttonIndex == 0)
       {
       }
    }
    if(alertView.tag==102)
    {
       if (buttonIndex == 1)
       {  
       }
    }
    }
    
    0 讨论(0)
提交回复
热议问题