延迟后使用选择器的多个动作

时间:2012-10-20 01:23:49

标签: iphone ios xcode sdk

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

2 个答案:

答案 0 :(得分: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];
    });
}

答案 1 :(得分:0)

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

-(void)performTheseAction {
    // do something
    // do something else
    [self callAnotherMethod];
    [self whyNotCallAnotherMethod];
}