关闭和打开窗口时暂停并恢复可重复的NSTimer

时间:2013-05-22 17:20:21

标签: objective-c nstimer nstableview nswindow awakefromnib

现在我知道有关于NSTimer暂停的问题,但在可重复和关闭/打开窗口时都没有。 Toggeling暂停工作完美(使用按钮检查)。 重新打开窗口会出现BAD_ACCESS错误。

- (void)awakeFromNib
{
    self.timer = [[NSTimer scheduledTimerWithTimeInterval:0.85
                                               target:self
                                             selector:@selector(populateTable:)
                                             userInfo:nil
                                              repeats:YES] retain];
}

-(IBAction)fireUpTableWindow:(id)sender
{
    [aTableWindow makeKeyAndOrderFront:self];
    [self resumeTimer:timer];
}


NSDate *pauseStart, *previousFireDate;

-(void) pauseTimer:timer
{
    pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];    
    previousFireDate = [[timer fireDate] retain];
    [timer setFireDate:[NSDate distantFuture]];
}

-(void) resumeTimer:timer
{
    float pauseTime = -1*[pauseStart timeIntervalSinceNow];
    [timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
    [pauseStart release];
    [previousFireDate release];
}

- (IBAction)closeTableWindow:(id)sender
{
    [self pauseTimer:timer];
    [self->aTableWindow close];
    [self release];
}
}

1 个答案:

答案 0 :(得分:0)

  

重新打开窗口会出现BAD_ACCESS错误。

在Interface Builder中检查Window未设置为关闭时释放