UIAlertView Click event inside UIAlertView delegate

17,259

Solution 1

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

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        //Do something
    }
    else
    {
        //Do something else
    }
}

Solution 2

Set the second alert view's delegate to nil:

UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                            message:nil delegate:nil 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];

Solution 3

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];
} 
}
Share:
17,259
Krunal
Author by

Krunal

iPhone/iPad developer from Mumbai (India) and you will always find me here to help you ;)

Updated on June 14, 2022

Comments

  • Krunal
    Krunal almost 2 years

    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.