iOS:在特定时间调用方法

发布于 2025-01-04 04:29:30 字数 687 浏览 1 评论 0原文

我这次尝试调用一个方法:00:01 AM 这是我的代码,但我找不到为什么此代码不调用我的方法的问题。

- (BOOL)date:(NSDate *)date hour:(NSInteger)h minute:(NSInteger)m {

    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:currentCalendar];

    NSDateComponents *componets = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit )fromDate:[NSDate date]];
    if ([componets hour ] == h && [componets minute] == m) {

        return YES;
    }
    return NO;
}



- (void)updateNewDate {

    if ([self date:[NSDate date] hour:4 minute:49]) {

        label.text = @"oyooy";
    }
}

- (void)viewDidLoad {

[self updateNewDate];
}

如果您还有其他方法请告诉我。

I am trying to call a method on this time : 00:01 AM here is my code but I can't find the problem why this code does not call my method.

- (BOOL)date:(NSDate *)date hour:(NSInteger)h minute:(NSInteger)m {

    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:currentCalendar];

    NSDateComponents *componets = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit )fromDate:[NSDate date]];
    if ([componets hour ] == h && [componets minute] == m) {

        return YES;
    }
    return NO;
}



- (void)updateNewDate {

    if ([self date:[NSDate date] hour:4 minute:49]) {

        label.text = @"oyooy";
    }
}

- (void)viewDidLoad {

[self updateNewDate];
}

If you have any other methods please tell me .

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

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

发布评论

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

评论(2

×眷恋的温暖 2025-01-11 04:29:30

实际上您的代码工作正常,但需要刷新...例如您可以使用 NSTimer
刷新你的方法:

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(updateNewDate) userInfo:nil repeats:YES];

Actually your code works fine but it needs refreshing ... for example you can use NSTimer
to refresh your method :

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(updateNewDate) userInfo:nil repeats:YES];
能否归途做我良人 2025-01-11 04:29:30

本地通知是要走的路......

编辑......
第二种方法是每当应用程序启动时获取当前时间并找到到下一个午夜的时间,然后启动一个计时器在该时间间隔后触发。
希望这有帮助。

Local Notification is the way to go....

EDIT...
Second way is to get the current time whenever the app launches and find the time till next midnight , then start a timer to fire after that time interval.
hope this helps.

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