Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在For循环中使用performSelector?苹果手机_Iphone_Objective C_Xcode_Multithreading_For Loop - Fatal编程技术网

Iphone 在For循环中使用performSelector?苹果手机

Iphone 在For循环中使用performSelector?苹果手机,iphone,objective-c,xcode,multithreading,for-loop,Iphone,Objective C,Xcode,Multithreading,For Loop,我有一个数组(在下面的代码中称为array),它包含许多MyView对象。我试图在For循环中迭代这些对象,并将它们逐个添加为子视图,每个延迟1秒后。下面代码的问题是,所有对象都是在延迟一秒钟后立即添加的。有人能建议我如何改正吗 先谢谢你 - (void)startMethod { for (MyView * myview in array) { [self performSelector:@selector(addSubView:) withObject:myvie

我有一个数组(在下面的代码中称为array),它包含许多MyView对象。我试图在For循环中迭代这些对象,并将它们逐个添加为子视图,每个延迟1秒后。下面代码的问题是,所有对象都是在延迟一秒钟后立即添加的。有人能建议我如何改正吗

先谢谢你

- (void)startMethod {

    for (MyView * myview in array) {

        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:1];
    }
}

- (void)addSubView : (UIView *)view {
    [soundController playSound];
    [self.view addSubview:view];
}

执行循环的时间不足以延迟选择器的执行。例如,您可能需要延迟使用计数器

-(void)startMethod {
    NSUInteger i = 0;
    for (MyView * myview in array) {
        i += 1;
        [self performSelector:@selector(addSubView:)
                   withObject:myview
                   afterDelay:i];
    }
}

其实很简单,

- (void)startMethod {
    int seconds = 0;
    for (MyView * myview in array) {
        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:++seconds];
    }
}

简单的解决方案是:在循环每次迭代后,将延迟增加1秒

- (void)startMethod {
    int seconds = 0;
    for (MyView * myview in array) {
        [self performSelector:@selector(addSubView:) withObject:myview afterDelay:++seconds];
    }
}