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

时间:2014-05-09 04:46:17

标签: objective-c uialertview

我已经阅读了一些使用警报视图.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"]];        
}
}

问题是委托仅适用于第一个警报。 我的应用程序如下:https://www.youtube.com/watch?v=WxlVKk0CTiQ

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

对于alert2,未调用Delegate,因为您尚未设置委托。

尝试进行以下更改。

报警1

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

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

委派

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

答案 2 :(得分:0)

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