Iphone 无效NSTimer不工作
我已经创建了一个计时器,其中我将剩余时间转换为格式为“mm:ss”的字符串,并且当时间的字符串值为00:00时,我希望使计时器无效。由于某些原因,它不起作用,即使在我记录剩余时间时,我看到的值具有正确的格式,因此我不知道为什么从未输入“countTime:”中的“if”分支 有人能帮忙吗Iphone 无效NSTimer不工作,iphone,objective-c,sdk,nstimer,stringwithformat,Iphone,Objective C,Sdk,Nstimer,Stringwithformat,我已经创建了一个计时器,其中我将剩余时间转换为格式为“mm:ss”的字符串,并且当时间的字符串值为00:00时,我希望使计时器无效。由于某些原因,它不起作用,即使在我记录剩余时间时,我看到的值具有正确的格式,因此我不知道为什么从未输入“countTime:”中的“if”分支 有人能帮忙吗 -(id) init { if(self = [super init]) { NSDate *date = [[NSDate alloc] init]; self.int
-(id) init {
if(self = [super init]) {
NSDate *date = [[NSDate alloc] init];
self.intervalLength = date;
[date release];
NSString *timeRem = [[NSString alloc]init];
self.timeRemaining = timeRem;
[timeRem release];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"mm:ss"];
notification = [NSNotification notificationWithName:@"timer" object:self];
notificationForEnd = [NSNotification notificationWithName:@"timerFinished" object:self];
NSMutableDictionary *info = [[NSMutableDictionary alloc] init];
[info setObject:formatter forKey:@"format"];
[info setObject:notification forKey:@"notification"];
[info setObject:notificationForEnd forKey:@"notificationEnd"];
NSTimer *timer = [[NSTimer alloc] init];
timer = [[NSTimer alloc] init];
timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countTime:) userInfo:info repeats:YES];
self.intervalTimer = timer;
}
return self;
}
-(void) startIntervalCountDown {
runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.intervalTimer forMode:NSDefaultRunLoopMode];
}
-(void) countTime:(NSTimer*)timer {
if(self.timeRemaining == @"00:00") {
[timer invalidate];
timer = nil;
[[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notificationEnd"]];
}
d = [self.intervalLength dateByAddingTimeInterval: -1.0];
self.intervalLength = [d copy];
self.timeRemaining = [[[timer userInfo] objectForKey:@"format"] stringFromDate:d];
[[NSNotificationCenter defaultCenter] postNotification:[[timer userInfo] objectForKey:@"notification"]];
您正在比较指针相等,而不是字符串相等。你想要
[self.timeRemaining isEqualToString:@"00:00"]