UIAlert View-for status yes / no

my application needs a warning msg, and if so, the button clicked another warning msg, and then I need to call the 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];
    }
}

Thank.

+3
source share
3 answers

I'm not sure what your goal is, but in any case everything looks wrong:

First of all, you should create your lines as follows:

NSString *title= @"Warning";

No need to use stringWithFormatin your case.

Then it seems to you that you did not configure the first UIAlert tag to be 1, and the default value for labels is 0, so I think that the operators ifin are didDismissWithButtonIndexnever true.

, , , buttonIndex, , [self resetArray] , .

, .

+1

, . :

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];

.

+1

Specify two separate UIAlertView files in the .h file

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

Now in your .m file, modify as shown 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 the dealloc method release the highlighted UIA images.

I hope you understand me.

Thanks,
Jim.

0
source

Source: https://habr.com/ru/post/1752944/


All Articles