tags:

views:

91

answers:

3

Hi friends,my app needs alert msg and if yes button pressed then one more alert msg and then i have to called a method.This is my code:

-(IBAction)resetPressed:(id)sender
{
    NSString *title= [NSString stringWithFormat:@"Warning"];
    NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
    NSString *ok = [NSString stringWithFormat:@"No"];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:self 
                                          cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
    [alert show];
    [alert release];
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==1)
    {
        NSString *title= [NSString stringWithFormat:@"Warning"];
        NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
        NSString *ok = [NSString stringWithFormat:@"No"];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self 
                                              cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
        alert.tag =2;
        [alert show];
        [alert release];

    }
    else if(alertView.tag ==2)
    {
        [self resetArray];
    }
}

Thanks.

A: 

Please define two separate UIAlertView in .h file

@interface XYZViewController:UIViewController
{
     UIAlertView  *firstAlertView;
     UIAlertView  *secondAlertView;
}

Now in your .m file modify as below:

-(IBAction)resetPressed:(id)sender
{
    NSString *title= [NSString stringWithFormat:@"Warning"];
    NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
    NSString *ok = [NSString stringWithFormat:@"No"];

    if(firstAlertView == nil)
    {
       firstAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
    }
    [firstAlertView show];

}

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView == firstAlertView)
    {
        NSString *title= [NSString stringWithFormat:@"Warning"];
        NSString *message = [NSString stringWithFormat:@"Are you sure you want to Reset"];
        NSString *ok = [NSString stringWithFormat:@"No"];

        if(secondAlertView == nil)
        {
            secondAlertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];
        }
        [secondAlertView show];
    }
    else if(alertView == secondAlertView)
    {
        [self resetArray];
    }
}

and in dealloc method please release the allocated UIAlertviews.

Hope i am clear to you.

Thanks,
Jim.

Jim
Thanks Jim.But for both button it is calling resetArray method.Please help me.Thanks.
1988
Do you want to call resetArray for both alert views?? or you want to call only for secondAlertView??
Jim
Thanks for reply.only for secondAlerView.
1988
The above implementation will only call ResetArray method on secondAlertView's dismiss event.I also checked with code. ResetArray method is only called for secondAlertView.
Jim
Thanks Jim.Your code works.I just made a minor change,inside the if and else if loop i wrote if(buttonIndex ==1) and its worked.Thanks.
1988
+1  A: 

Hi,

I'm not sure what your goal is but a few things look wrong to me anyways:

First of all you should create your strings this way:

NSString *title= @"Warning";

There's no need to use stringWithFormat in your case.

Then, it doesn't seem you properly set the first UIAlert's tag to 1, and the default value for tags is 0 so I guess the if statements in didDismissWithButtonIndex are never true.

Also, you should check which button was pressed using buttonIndex, otherwise you are going to show both alert and call [self resetArray] whichever button is pressed by the user.

Hope that helps.

Jukurrpa
Thanks Jukurpa for your suggestion it worked.I checked buttonIndex n problem solved.
1988
Glad I could help. Don't forget to accept the answer so this question is marked as answered.
Jukurrpa
I notice that this great answer isn't accepted, yet. It's nearly exactly the answer I was thinking in my head when I was reading the question. Either way, I voted it up.Pallavi, don't forget to ACCEPT the answer. This is how you say "thank you" to people on StackOverflow :)
Olie
A: 

In your code, you create the first alert, but never actually set the tag on it. You should do:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:message
                                                   delegate:self 
                                          cancelButtonTitle:ok otherButtonTitles:@"Yes",nil];

alert.tag = 1; //Or 2, or something.
[alert show];
[alert release];

Then the code in your delegate method will run.

Wayfarer