iOS:在特定时间调用方法
我试图在这个时间调用一个方法: 00:01 AM
: 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]; }
如果您有任何其他方法,请告诉我。
实际上你的代码工作正常但它需要刷新…例如你可以使用NSTimer
来刷新你的方法:
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(updateNewDate) userInfo:nil repeats:YES];
本地通知是要走的路….
编辑……第二种方法是在应用程序启动时获取当前时间并找到下一个午夜的时间,然后在该时间间隔后启动计时器。 希望这可以帮助。
- 如何在iOS Phonegap应用程序中显示自定义的飞溅屏幕微调(即微调白色)?
- 如何将像“2011-01-12T14:17:55.043Z”这样的datestring转换为像1294841716那么长的string?
- 录制audio时如何将audio录制设置更改为16Khz和16位?
- 如何从iPhone摄像头使用图像Exif元数据获取对象的距离?
- 我如何parsing这个XML在NSXMLParser在iOS中?
- 如何用date时间值sortingNSArray?
- 从iOS的Temp目录中删除文件
- 如何比较两个实际上等价但具有装饰性string差异的NSURL?
- 设备旋转后,UITableView纠正滚动位置