如何设置时间延迟以从一个接一个地从数组中显示alertview?

How to set time delay for displaying the alertview from one after another From an array?

提问人:Purushothaman 提问时间:4/26/2016 最后编辑:Nirav GadhiyaPurushothaman 更新时间:7/21/2016 访问量:98

问:

如何设置时间延迟以从一个接一个地从数组中显示alertview?我在一个数组中有 5 个 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];
        }
iOS Objective-C iPhone Cocoa AlertView

评论

0赞 Jayesh Thanki 4/26/2016
使用此代码时有什么问题?请解释更多,以便我为您提供帮助。

答:

0赞 Nirav Gadhiya 4/26/2016 #1

试试这个......

-(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];
    }
0赞 Striker 4/26/2016 #2

使用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++;

}