Two Alert Views in One View Controller - buttonIndex Response

孤街醉人 提交于 2019-12-04 01:28:34

问题


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 the View Controller. I am concerned that if I duplicated the code, my buttonIndex would not know which alert it is responding to. Any ideas? Thanks!

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

回答1:


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



回答2:


set tag to alert view.

alert.tag = 1;

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



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/14621891/two-alert-views-in-one-view-controller-buttonindex-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!