Pregunta

I have read some methods using alert view .tag, but that doesn't work.

The first alert:

-(void)addNewTask
{

    UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:@"New Task" 
    message:nil       
    delegate:self 
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
    alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert1 show];
}

The Second Alert

-(void)changeTime:(int)value atRow:(int)p
{

    TaskData *data = [tasks objectAtIndex:p];

    NSLog([NSString stringWithFormat:@"%d %d",data.time,value]);


    int time = data.time ;

    time += value;

    data.time = time;

    NSLog([NSString stringWithFormat:@"%d %d",data.time,value]);



[self saveData:tasks];

[self.Taskview reloadData];

if(time>=5&&(data.leveljudge!=1))
{
    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Congratulation!!!"
     message:@"You have achieve Senior Level. "
     delegate:nil
     cancelButtonTitle:@"Cancel" otherButtonTitles:@"YES",nil];



    data.leveljudge=1;
    [alert2 show];

}

Delegate:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *name = [alertView buttonTitleAtIndex:buttonIndex];

if ([name isEqualToString:@"OK"])
{
    UITextField *tf=[alertView textFieldAtIndex:0];

    NSString *name = tf.text;
    TaskData *newTask = [[TaskData alloc] init];
    newTask.TaskName = name;
    newTask.time = 0;
    newTask.leveljudge=0;
    [tasks addObject:newTask];

    [self saveData:tasks];

    [self.Taskview reloadData];

}

else if ([name isEqualToString:@"YES"]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://d.blog.xuite.net/d/1/5/4/12103250/blog_1606564/txt/53796893/2.jpg"]];        
}
}

The problem is the delegate only work for the first alert. My app looks like: https://www.youtube.com/watch?v=WxlVKk0CTiQ

¿Fue útil?

Solución 2

For alert2, Delegate is not called because you have not set the delegate.

Try making the following changes.

Alert1

UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:@"New Task" 
                                                   message:nil       
                                                 delegate:self 
                       cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
alert1.alertViewStyle = UIAlertViewStylePlainTextInput;
alert1.tag = 101; //Add a Tag to the alert

[alert1 show];

Alert2 Alert 2 delegate is not set properly

UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Congratulation!!!"
                           message:@"You have achieve Senior Level. "
                          delegate:self //Set your delegate 
 cancelButtonTitle:@"Cancel" otherButtonTitles:@"YES",nil];


alert2.tag = 102; //Add a different tag value to the second alert
data.leveljudge=1;
[alert2 show];

Delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 101)
    {
         //Handle Alert 1
    }
    else if(alertView.tag ==102)
    {
         //Handle Alert 2
    }
}

Otros consejos

-(IBAction)flipAction:(id)sender
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"This is My Button" message:@" Hi" delegate:self cancelButtonTitle:@"OK " otherButtonTitles:@"Cancel", nil];
    alert.tag=1;
    [alert show];
    [alert release];
}

-(IBAction)flipAction123:(id)sender
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"This is a flipAction123" message:@"this is message..." delegate:self cancelButtonTitle:@"OK " otherButtonTitles:@"Cancel", nil];
    alert.tag=2;
    [alert show];
    [alert release];

}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

{
    if (alertView.tag==1)
    {
        if (buttonIndex==0)
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"This is another alert button" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
        }
        else
        {
            NSLog(@"paresh here..");
        }

    }
    else if(alertView.tag==2)
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"This is another alert alertView.tag ==2" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
    }
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Congratulation!!!"
     message:@"You have achieve Senior Level. "
     delegate:self  //here should be self, not nil
     cancelButtonTitle:@"Cancel" otherButtonTitles:@"YES",nil];
data.leveljudge=1;
[alert2 show];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top