如何设置一个接一个地显示警报视图的时间延迟从一个数组?

时间:2016-04-26 07:14:59

标签: ios objective-c iphone cocoa alertview

如何设置一个接一个地显示警报视图的时间延迟从一个数组?我在一个数组中有5个alertview ..每个alertview将在一段时间后显示。

     for (NSDictionary *temp in [RMUserDefaults userDetails].SharedFolders)
        {
          NSString *name = temp[@"Name"];          
          alert1= [[CustomUIAlertView alloc]initWithTitle:@"message" message:[NSString stringWithFormat:@"Message.", name] delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:nil]]; 
          [alert1 show];
        }

2 个答案:

答案 0 :(得分:0)

试试这个......

-(void)test{
    [self showAlertWithIndex:0];
}

-(void)showAlertWithIndex:(NSInteger)index
{
    NSArray * arr = [RMUserDefaults userDetails].SharedFolders;
    NSString *name = arr[index][@"Name"];
    alert1= [[CustomUIAlertView alloc]initWithTitle:@"message" message:[NSString stringWithFormat:@"Message.", name] delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:nil]];
    alert1.tag = index;
    [alert1 show];
    [self performSelector:@selector(dismissAlertView:) withObject:alert1 afterDelay:2.0f];
    // set delay in above line
}

-(void)dismissAlertView:(UIAlertView *)alert
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    NSInteger index = alert.tag+1;

    NSArray * arr = [RMUserDefaults userDetails].SharedFolders;
    if (index < arr.count)
    {
        [self showAlertWithIndex:index];
    }
}

编辑:

您需要在alertview委托中编写以下代码

    NSInteger index = alert.tag+1;

    NSArray * arr = [CustomUIAlertView userDetails].SharedFolders;
    if (index < arr.count)
    {
        [self showAlertWithIndex:index];
    }

答案 1 :(得分:0)

使用dispatch_after

int i = 1;

for (NSDictionary *temp in [RMUserDefaults userDetails].SharedFolders) {
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, i * delaySeconds * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
        NSString *name = temp[@"Name"]; 
        alert1= [[CustomUIAlertView alloc]initWithTitle:@"message" message:[NSString stringWithFormat:@"Message.", name] delegate:self cancelButtonTitle:@"CANCEL" otherButtonTitles:nil]];
        [alert1 show];
    });

    i++;

}