Two types of alerts in one controller view - buttonIndex Response

I am trying to complete the smile task by having two warnings in the same View controller. The code below works fine, but how would I make another instance of it elsewhere in the view controller. I am worried that if I duplicate the code, my Index button will not know which signal it is responding to. Any ideas? Thank you

-(void)alertChoice
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) 
    {
    //do something
    }
}
+5
source share
4 answers

You can use the property tagon UIAlertViewto decrypt which warning:

-(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)
    {
    }
}
+25
source

set a tag to view alerts.

alert.tag = 1;

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1 && alertView.tag == 1) 
    {
    //do something
    }
}
+2
source

, messeg.

alertView.tag=0;

-(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
    }
  }
+2

Step 1: add the UIAlertViewDelegate to your file. controller.h file

step 2: add the following methods to your 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 two above methods in your viewController, as shown below: [self AlertMethodOne]; [self AlertMethodTwo];

Now the click method of the AlertView button

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag==101)
{
   if (buttonIndex == 0)
   {
   }
}
if(alertView.tag==102)
{
   if (buttonIndex == 1)
   {  
   }
}
}
+1
source

All Articles