0

- (void)displayView:(UIView *)view arg:(UILabel *)label { }在延迟 0.5 秒后,我将如何调用此方法?

我已经尝试过[self performSelector:@selector(displayView:) withObject:view afterDelay:.5];,但这只会将视图作为参数传递。

如何将两个参数传递给延迟为 0.5 秒的方法?

4

2 回答 2

6

使用延迟块调用:

主线程:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self displayView:view arg:label];
});

后台线程:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self displayView:view arg:label];
});
于 2013-06-18T16:38:08.607 回答
-2
- (id)performSelector:(SEL)aSelector
           withObject:(id)anObject  
           withObject:(id)anotherObject

来自苹果文档

[self performSelector:@selector(displayView:arg:)
           withObject:myView
           withObject:myLabel];

但我真的建议您使用GCD

另一个选项是dispatch_after,它将占用一个块并在特定时间将其排入队列。

double delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    [self displayView:myView arg:myLabel];

});

另一种方法是使用NSTimer

myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
    target:self
    selector:@selector(displayViewWitharg:)
    userInfo:dict
    repeats:NO];

// where dict is NSDictioary with key-value pair, which contains your parameters which you want to pass.

在这里,只要您需要,您就可以使此计时器无效以停止该过程。

希望对你有帮助

于 2013-06-18T16:39:52.940 回答