使来自其他ViewController的NSTimer无效



在我的项目中,有3个视图控制器,其中1vc有所有产品的清单及其关闭日期,2vc将显示带有倒计时定时器的产品细节,并有1个按钮在那里投标该项目,3VC将显示投标金额和所有其他细节与投标按钮。

所以它的工作原理类似于VC1=>VC2=>VC3。

所以从VC3我回到VC2,它应该运行定时器(当VC2在后台时,它应该在后台运行)。

但当我转到VC1到VC2时,我需要从后台删除计时器,并用另一个产品的新倒计时设置新计时器。。我添加了类似的NSTimer

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

在VC2中,我有

{
    NSTimer *timer;
}
- (void)updateCounter:(NSTimer *)theTimer {
    if(secondsLeft > 0 ){
        secondsLeft -- ;
        days = (secondsLeft / 3600) / 24;
        int reminder = secondsLeft % 84600;
        hours = (reminder / 3600);
        reminder = (reminder % 3600);
        minutes = (reminder) / 60;
        reminder = reminder % 60;
        seconds = reminder;
        self.headerTimeLastCallLbl.text = [NSString stringWithFormat:@"%02d", hours];
        self.headerTimeHourLbl.text = [NSString stringWithFormat:@"%02d", minutes];
        self.headerTimeMinutesLbl.text = [NSString stringWithFormat:@"%02d", seconds];
        if ([[[self.auctionItemDetail objectAtIndex:0] valueForKey:@"daysLeft"] integerValue] == 0) {
            self.headerTimeLastCallLbl.text = [NSString stringWithFormat:@"%d Hours", hours];
            self.headerTimeHourLbl.text = [NSString stringWithFormat:@"%d Minutes", minutes];
            self.headerTimeMinutesLbl.text = [NSString stringWithFormat:@"%d Seconds", seconds];
        }else{ 
            self.headerTimeLastCallLbl.text = [NSString stringWithFormat:@"%d Days", days];
            self.headerTimeHourLbl.text = [NSString stringWithFormat:@"%d Hours", hours];
            self.headerTimeMinutesLbl.text = [NSString stringWithFormat:@"%d Minutes", minutes];
        }
    }
}
-(void)countdownTimer{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

我在viewDidLoad中添加了删除代码,就像这个一样

@try {
        [timer invalidate];
        NSLog(@"Timer invalidated");
    }
    @catch (NSException *exception) {
        NSLog(@"Timer invalidated with exception");
    }
    @finally {
    }

但计时器仍在后台运行。。所以当我第二次访问VC2时,会有2个计时器在后台运行(因为每次访问VC2计时器都会添加到mainRunLoop中)。。我想删除第一个计时器。。

已编辑

我在singleton类中添加了定时器

Constant.h
#import <Foundation/Foundation.h>
@interface Constant : NSObject
{}
+ (NSTimer *) aucNSTimer;
@end

Constant.m
#import "Constant.h"
@implementation Constant
+ (NSTimer *) aucNSTimer
{
    static NSTimer *timer = nil;
    @synchronized(self) {
        if (timer == nil)
            timer = [[NSTimer alloc] init];
    }
    return timer;
}
@end

VC2.h
{
    NSTimer *timer;
}

VC2.m

- (void)viewDidLoad {
timer = [Constant aucNSTimer];
@try {
        [[Constant aucNSTimer] invalidate];
        [timer invalidate];
        NSLog(@"Timer invalidated");
    }
    @catch (NSException *exception) {
        NSLog(@"Timer invalidated with exception");
    }
    @finally {
    }
}

无需在mainRunLoop 中添加计时器

-(void)countdownTimer
{
  timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}

如果你想无效定时器,请遵循以下代码:

if (timer)
{
   [timer invalidate];
   timer = nil;
}

并在dealloc方法中实现此代码

-(void)dealloc
{
  if (timer)
   {
    [timer invalidate];
    timer = nil;
   }
}

如果要在代码中启用ARC,请不要编写[super dealloc];

最新更新