无效 NSTimer 不工作

发布于 2024-09-12 13:30:09 字数 1956 浏览 3 评论 0原文

我创建了一个计时器,将剩余时间转换为格式为“mm:ss”的字符串,当时间的字符串值为 00:00 时,我想使计时器无效。由于某种原因,它不起作用,即使当我记录剩余时间时,我也看到该值具有正确的格式,所以我不知道为什么我的“countTime:”中的“if”分支从未被输入。

有人可以帮忙吗?

-(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"]];

I am have created a timer where I convert the remaining time to a string with a format of:"mm:ss" and when the string value of the time is 00:00 I would like to invalidate the timer. For some reason it doesn't work, even when I log the remaining time I see the the value has the correct format so I don't know why my "if" branch in "countTime:" is never entered.

Can anyone help?

-(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"]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2024-09-19 13:30:09

您正在比较指针相等性而不是字符串相等性。你想要

[self.timeRemaining isEqualToString:@"00:00"]

You're comparing pointer equality rather than string equality. You want

[self.timeRemaining isEqualToString:@"00:00"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文