Two Alert Views in One View Controller - buttonIndex Response

前端 未结 4 1165
日久生厌
日久生厌 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: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)
       {  
       }
    }
    }
    

提交回复
热议问题