7

I am new in iPhone developer,

I want to implement 2 alert view one after another, like when user press delete button, 1st alert view will ask Are you sure want to Delete ? with two buttons yes and no

Now, if user presses yes , then 2nd alert view will come with message Deleted Successfully ! this alert view contains only OK button, now on click of this OK button i want to call one method.

and if user presses No then nothing should happen and alert should dismiss.

Here is my code snippet,

-(void)DeletebtnCliked:(id)sender
{   
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure want to delete ?"
                                                        message:nil delegate:self 
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Yes",@"No",nil];
    [alertView show];
    [alertView release];   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
    if (buttonIndex == 0)
    {            
         UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                           message:nil delegate:self 
                                                 cancelButtonTitle:@"OK"
                                                 otherButtonTitles:nil];
        [alertew show];
        [alertew release];

        if (buttonIndex == 0)
        {
            [self MethodCall];
        }
    }
    else if (buttonIndex == 1)
    {
        [alertView dismissWithClickedButtonIndex:1 animated:TRUE];
    } 
}

after writing this code i am inside Infinite loop.

Any help will be appreciated.

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Krunal
  • 6,440
  • 21
  • 91
  • 155

4 Answers4

11
alertView.tag = 1;
alertew.tag = 2;

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        //Do something
    }
    else
    {
        //Do something else
    }
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Jesse Gumpo
  • 4,777
  • 1
  • 20
  • 29
4

Set the second alert view's delegate to nil:

UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                            message:nil delegate:nil 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];
citydeer
  • 591
  • 3
  • 6
1

Either use tags to tackle the situation like following or simply just set Delegate nil for the inner alertView which is inside the delegate methode so that it will never call.

-(void)DeletebtnCliked:(id)sender
{   
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Are you sure want to delete ?"
                                                    message:nil delegate:self 
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"Yes",@"No",nil];
alertView.tag = 1;
[alertView show];
[alertView release];   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
if (buttonIndex == 0 && alertView.tag == 1)
{            
     UIAlertView* innerAlert = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                       message:nil delegate:nil 
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
    innerAlert.tag = 2;
    [innerAlert show];
    [innerAlert release];

    if (buttonIndex == 0 && alertView.tag == 1)
    {
        [self MethodCall];
    }
}
else if (buttonIndex == 1 && alertView.tag == 1)
{
    [alertView dismissWithClickedButtonIndex:1 animated:TRUE];
} 
}
superGokuN
  • 1,399
  • 13
  • 28
0

try this:-

-(void)button:(UIButton *)buttonDelete{
           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"delete" message:@"Do you Want to delete" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil];
            alertView.delegate = self;
            alertView.tag = 2000;
            [alertView show];
         }
-(void)buttonUpdate:(UIButton *)buttonEdit{

        UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:@"Update" message:@"Do you Want to Update" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil];
            alertView2.delegate = self;
            alertView2.tag = 20001;
            [alertView show];
    }   
 -(void)buttonAdd:(UIButton *)buttonAdd{
         UIAlertView *alertView3 = [[UIAlertView alloc] initWithTitle:@"Add" message:@"Do you Want to Add" delegate:self cancelButtonTitle:@"Cancle" otherButtonTitles:@"Yes", nil];
            alertView3.delegate = self;
            alertView3.tag = 20002;
            [alertView show];
    }     
  -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
            if (alertView.tag == 2000){
                if (buttonIndex==0) {
                    NSLog(@"Delete Cancel Button click");
                }
                else{
                    NSLog(@"Delete yes Button is click");
                }

            }
            if (alertView.tag == 20001){
                if (buttonIndex==0) {
                    NSLog(@"update Cancel Button click");
                }
                else{
                    NSLog(@"update yes Button is click");
                }

            }
            if (alertView.tag == 20002){
                if (buttonIndex==0) {
                    NSLog(@"Add Cancel Button click");
                }
                else{
                    NSLog(@"Add yes Button is click");
                }

            }
        }