Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView Click event inside UIAlertView delegate

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.

like image 605
Krunal Avatar asked Jul 10 '12 08:07

Krunal


3 Answers

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

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2)
    {
        //Do something
    }
    else
    {
        //Do something else
    }
}
like image 140
Jesse Gumpo Avatar answered Nov 12 '22 11:11

Jesse Gumpo


Set the second alert view's delegate to nil:

UIAlertView* alertew = [[UIAlertView alloc] initWithTitle:@"Deleted Successfully !"
                                                            message:nil delegate:nil 
                                                  cancelButtonTitle:@"OK" otherButtonTitles:nil];
like image 4
citydeer Avatar answered Nov 12 '22 11:11

citydeer


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];
} 
}
like image 1
superGokuN Avatar answered Nov 12 '22 11:11

superGokuN