0

我不明白为什么这不起作用。我试图在计时器周期被击中时显示 lblstatus :

-(void) viewDidAppear:(BOOL)animated
{
        [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(TimeForPictureCycle)
                                       userInfo:nil
                                        repeats:NO];
}

-(void)TimeForPictureCycle
{
    while(YES)
    {
        [lblStatus setHidden:NO];
        sleep(2);
        [lblStatus setHidden:YES];
        sleep(3);
    }
}
4

2 回答 2

3

您在主线程上的调用sleep(),这意味着在您将标签设置hidden为该线程后会暂停并阻止运行循环继续,因此它无法更新您的 UI。您当前的代码简单地阻塞了您的整个应用程序。

你可以做的是:

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.showTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                         target:self
                                       selector:@selector(showLabel)
                                       userInfo:nil
                                        repeats:NO];
}

-(void)showLabel
{ 
    [lblStatus setHidden:NO];
    self.hideTimer = [NSTimer scheduledTimerWithTimeInterval:2
                                         target:self
                                       selector:@selector(hideLabel)
                                       userInfo:nil
                                        repeats:NO];
}

-(void)hideLabel
{        
    [lblStatus setHidden:YES];
    self.showTimer = [NSTimer scheduledTimerWithTimeInterval:3
                                         target:self
                                       selector:@selector(showLabel)
                                       userInfo:nil
                                        repeats:NO];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.showTimer invalidate];
    [self.hideTimer invalidate];
}
于 2012-11-04T22:51:33.787 回答
1

你可以简单地做到这一点:

-(void)viewDidAppear:(BOOL)animated {
    [lblStatus performSelector:@selector(setHidden:) withObject:0 afterDelay:1];
    [lblStatus performSelector:@selector(setHidden:) withObject:@1 afterDelay:3];
}

我不太确定为什么我可以将 0 作为对象传递,但它确实有效。

于 2012-11-04T23:57:53.020 回答