0

有没有办法分别使用: performSelector:withObject:afterDelay 代码执行多个操作?示例代码将不胜感激,在此先感谢。

4

2 回答 2

1

或者使用块。如果您开始键入dispatch_after,您将看到代码完成,该代码将弹出以下代码片段,然后您可以在该块中放置任何您想要的操作。在这个例子中,我展示了它在一个内部使用IBAction

- (IBAction)pushedSomeButton:(id)sender
{
    // anything you want to do immediate, do here

    [self doingSomethingRightNow];

    // anything you want to defer for some time, do inside the dispatch_after block
    // in this example, calling callAnotherMethod and whyNotCallAnotherMethod

    int64_t delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self callAnotherMethod];
        [self whyNotCallAnotherMethod];
    });
}
于 2012-10-20T02:47:43.420 回答
0

设置一个使用 performSelector:withObject:afterDelay: 调用触发的方法:

-(void)performTheseAction {
    // do something
    // do something else
    [self callAnotherMethod];
    [self whyNotCallAnotherMethod];
}
于 2012-10-20T01:58:16.877 回答