需要在日期改变时显示日期的数字也随之改变吗?
我需要创建一个循环来检测 24 小时过去后,日历上的数字从 1 变为 2,然后从 2 变为 3 ... 一直到 31。所以当它是 31 时,它会显示 31。但是数字是在我的选项卡栏符号上,我已经使用石英绘制了数字,因此该数字不是整数,而是带有写入数字的 nsstring 。显示的数字是 6,而它应该是今天的日期 17。不确定我哪里出错了?
到目前为止,这是我的代码:
- (void)updateLabelForDate:(NSDate *)date {
NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
NSInteger days = timeInterval / (60*60*24);
NSArray *sloganArray = [NSArray arrayWithObjects:
NSLocalizedString(@"1", nil),
NSLocalizedString(@"2", nil),
NSLocalizedString(@"3", nil),
NSLocalizedString(@"4", nil),
NSLocalizedString(@"5", nil),
NSLocalizedString(@"6", nil),
NSLocalizedString(@"7", nil),
NSLocalizedString(@"8", nil),
NSLocalizedString(@"9", nil),
NSLocalizedString(@"10", nil),
NSLocalizedString(@"11", nil),
NSLocalizedString(@"12", nil),
NSLocalizedString(@"13", nil),
NSLocalizedString(@"14", nil),
NSLocalizedString(@"15", nil),
NSLocalizedString(@"16", nil),
NSLocalizedString(@"17", nil),
NSLocalizedString(@"18", nil),
NSLocalizedString(@"19", nil),
NSLocalizedString(@"20", nil),
NSLocalizedString(@"21", nil),
NSLocalizedString(@"22", nil),
NSLocalizedString(@"23", nil),
NSLocalizedString(@"24", nil),
NSLocalizedString(@"25", nil),
NSLocalizedString(@"26", nil),
NSLocalizedString(@"27", nil),
NSLocalizedString(@"28", nil),
NSLocalizedString(@"29", nil),
NSLocalizedString(@"30", nil),
NSLocalizedString(@"31", nil),
nil];
NSInteger usedSloganIndex = (int)days % [sloganArray count];
slogan = [sloganArray objectAtIndex:usedSloganIndex];
NSLog(@"Slogan: %@", slogan);
NSLog(@"%i",usedSloganIndex);
int x,y,width,height;
x = 23;
y = 440;
width = 20;
height = 20;
CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:slogan];
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[self updateLabelForDate:[NSDate date]];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self updateLabelForDate:[NSDate date]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[self addTabBarArrow];
[window makeKeyAndVisible];
[self updateLabelForDate:[NSDate date]];
return YES;
}
谢谢
I need to create a loop that detects when 24 hours passes, the number changes on my calendar from 1 to 2 then 2 to 3 ... all the way to 31. So when it is the 31 it says 31. However the number is on my tab-bar symbol and I have drawn the number on using quartz and so the number is not an integer but an nsstring with the number written. the number that is displayed is 6 when it should be today's date 17. Not sure where i am going wrong?
Here is my code so far:
- (void)updateLabelForDate:(NSDate *)date {
NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
NSInteger days = timeInterval / (60*60*24);
NSArray *sloganArray = [NSArray arrayWithObjects:
NSLocalizedString(@"1", nil),
NSLocalizedString(@"2", nil),
NSLocalizedString(@"3", nil),
NSLocalizedString(@"4", nil),
NSLocalizedString(@"5", nil),
NSLocalizedString(@"6", nil),
NSLocalizedString(@"7", nil),
NSLocalizedString(@"8", nil),
NSLocalizedString(@"9", nil),
NSLocalizedString(@"10", nil),
NSLocalizedString(@"11", nil),
NSLocalizedString(@"12", nil),
NSLocalizedString(@"13", nil),
NSLocalizedString(@"14", nil),
NSLocalizedString(@"15", nil),
NSLocalizedString(@"16", nil),
NSLocalizedString(@"17", nil),
NSLocalizedString(@"18", nil),
NSLocalizedString(@"19", nil),
NSLocalizedString(@"20", nil),
NSLocalizedString(@"21", nil),
NSLocalizedString(@"22", nil),
NSLocalizedString(@"23", nil),
NSLocalizedString(@"24", nil),
NSLocalizedString(@"25", nil),
NSLocalizedString(@"26", nil),
NSLocalizedString(@"27", nil),
NSLocalizedString(@"28", nil),
NSLocalizedString(@"29", nil),
NSLocalizedString(@"30", nil),
NSLocalizedString(@"31", nil),
nil];
NSInteger usedSloganIndex = (int)days % [sloganArray count];
slogan = [sloganArray objectAtIndex:usedSloganIndex];
NSLog(@"Slogan: %@", slogan);
NSLog(@"%i",usedSloganIndex);
int x,y,width,height;
x = 23;
y = 440;
width = 20;
height = 20;
CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:slogan];
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[self updateLabelForDate:[NSDate date]];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self updateLabelForDate:[NSDate date]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the tab bar controller's view to the window and display.
[window addSubview:tabBarController.view];
[self addTabBarArrow];
[window makeKeyAndVisible];
[self updateLabelForDate:[NSDate date]];
return YES;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在应用程序委托中实现
applicationSignificantTimeChange:
,您可以在日期更改时收到通知。检查其文档了解何时会被调用,但它包括您几乎肯定想要的时间。You can be notified when the date changes by implementing
applicationSignificantTimeChange:
in your application delegate. Check its documentation for when it will be called, but it includes the times you almost certainly want.