Ios 从其他ViewController使NSTimer无效
在我的项目中,有3个视图控制器,其中 1vc有所有产品的清单及其截止日期, 2vc将使用倒计时显示产品详细信息,并在那里有一个按钮对该项目进行投标, 3VC将通过“投标”按钮显示投标金额和所有其他详细信息 所以它的工作原理类似于VC1=>VC2=>VC3 所以从VC3我回到VC2,它应该运行定时器(当VC2在后台时,它应该在后台运行) 但当我转到VC1到VC2时,我需要从后台删除计时器,并使用另一个产品的新倒计时设置新计时器。。 我像这样加了N定时器Ios 从其他ViewController使NSTimer无效,ios,objective-c,timer,nstimer,Ios,Objective C,Timer,Nstimer,在我的项目中,有3个视图控制器,其中 1vc有所有产品的清单及其截止日期, 2vc将使用倒计时显示产品详细信息,并在那里有一个按钮对该项目进行投标, 3VC将通过“投标”按钮显示投标金额和所有其他详细信息 所以它的工作原理类似于VC1=>VC2=>VC3 所以从VC3我回到VC2,它应该运行定时器(当VC2在后台时,它应该在后台运行) 但当我转到VC1到VC2时,我需要从后台删除计时器,并使用另一个产品的新倒计时设置新计时器。。 我像这样加了N定时器 [[NSRunLoop mainRunLoo
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
@try {
[timer invalidate];
NSLog(@"Timer invalidated");
}
@catch (NSException *exception) {
NSLog(@"Timer invalidated with exception");
}
@finally {
}
在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
中添加了如下删除代码
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
@try {
[timer invalidate];
NSLog(@"Timer invalidated");
}
@catch (NSException *exception) {
NSLog(@"Timer invalidated with exception");
}
@finally {
}
但计时器仍在后台运行。。所以当我第二次访问VC2时,会有两个计时器在后台运行(因为每次访问VC2时,计时器都会添加到mainRunLoop中)。。在那里,我想删除第一个计时器
已编辑
我在singleton类中添加了计时器
Constant.h
#import <Foundation/Foundation.h>
@interface Constant : NSObject
{}
+ (NSTimer *) aucNSTimer;
@end
。
VC2.m
不需要在mainRunLoop中添加计时器
-(void)countdownTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
如果您想禁用计时器,请遵循以下代码:
if (timer)
{
[timer invalidate];
timer = nil;
}
并在dealoc方法中实现此代码
-(void)dealloc
{
if (timer)
{
[timer invalidate];
timer = nil;
}
}
不要写
[super dealoc]代码>如果要在代码中启用ARC。只需删除[[nsrunlop main runloop]addTimer:timer forMode:NSRunLoopCommonModes]代码>因为scheduledTimerWithTimeInterval
如果设置重复:是,则设置带循环的计时器。
-(void)dealloc
{
if (timer)
{
[timer invalidate];
timer = nil;
}
}