Objective c 一个视图控制器中的多个警报视图(标记不起作用)

Objective c 一个视图控制器中的多个警报视图(标记不起作用),objective-c,uialertview,Objective C,Uialertview,我已经阅读了一些使用alert view.tag的方法,但这不起作用 第一个警报: -(void)addNewTask { UIAlertView * alert1 = [[UIAlertView alloc] initWithTitle:@"New Task" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil]; aler

我已经阅读了一些使用alert view.tag的方法,但这不起作用

第一个警报:

-(void)addNewTask
{

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

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

}
代表:

-(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"]];        
}
}
问题是代理仅对第一个警报工作。
我的应用程序如下所示:

对于alert2,未调用代理,因为您尚未设置代理

尝试进行以下更改

警报1

警报2 警报2委托未正确设置

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


您没有设置第二个警报代理。UIAlertView*alert2=[[UIAlertView alloc]initWithTitle:@“恭喜!!!”消息:@“您已达到高级。”代表:无取消按钮:@“取消”其他按钮:@“是”,无];中国代表:赛尔夫。代理将起作用。使用
标记
起作用。你在哪里尝试使用标签?谢谢。我现在明白了,谢谢。它正在工作。我犯了一个愚蠢的错误。
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];
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];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 101)
    {
         //Handle Alert 1
    }
    else if(alertView.tag ==102)
    {
         //Handle Alert 2
    }
}
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];