viewDidLoad
方法中)。问题是,每当我导航到同一个ViewController时,它就会再次执行洗牌,而我只需要它每天洗牌一次。因此,我需要检查此ViewController是否在同一天(即从午夜开始)被加载过,然后我可以将洗牌放入if语句中。我能否在午夜安排洗牌,而不管应用程序是否打开?
我已经研究了将布尔值设置为
NSUserDefaults
:类似于hasLoadedSinceMidnight
,但是无法弄清如何在午夜重置布尔值。viewDidLoad
方法中)。问题是,每当我导航到同一个ViewController时,它就会再次执行洗牌,而我只需要它每天洗牌一次。NSUserDefaults
:类似于hasLoadedSinceMidnight
,但是无法弄清如何在午夜重置布尔值。您可以实现AppDelegate的significantTimeChange方法:
-(void)applicationSignificantTimeChange:(UIApplication *)application {
//tell your view to shuffle
}
这个方法会在每天午夜和重大时间更改(如时区更改)时被调用。如果您的应用程序在事件接收时已关闭,则下次打开应用程序时将调用此方法。
更多信息可以在这里查看。
在ViewController中执行相同操作的另一种方法是添加:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:UIApplicationSignificantTimeChangeNotification object:nil];
然后,您可以在该ViewController的-(void)performAction;
方法中执行您的洗牌操作。
UIApplicationSignificantTimeChangeNotification
С╗ЦУјитЈќ+1ТѕќТЏ┤тЦйуџёу╝ќуеІСйЊжфїсђѓ - vikingosegundolabel.text = @"SomeText"
进行更改? - Jsdodgers不要存储一个BOOL
,而是可以存储上次洗牌的日期/时间(NSDate
)。
然后在viewDidAppear
中通过比较存储的日期和当前日期来检查是否已经过了午夜。
以及NSDateFormatter
文档:
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003643
更新:
根据要求,这里提供一些示例代码。诚然,可能有更好的解决方案,但我相信这段代码片段将帮助您解决问题。它的作用是检查是否使用NSUserDefaults
保存了日期,然后与当前日期进行比较。如果日期不匹配,则对数组进行洗牌,然后保存当前日期(再次使用NSUserDefaults
)。 (我假设时间确实会继续向前移动,因此它不会检查lastSavedDate
是否在currentDate
之前。)
NSDate *currentDate = [[NSDate alloc] init];
NSDate *lastShuffleDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastShuffleDate"];
// check to see if there is a prior shuffle date
// if there is not, shuffle the array and save the current date
if (!lastShuffleDate) {
NSLog(@"No object set for 'lastShuffleDate'");
//[self shuffleMyArray];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];
return;
}
// set up the date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(@"Current Date: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Date: %@", [dateFormatter stringFromDate:lastShuffleDate]);
// check to see if the dates are the same by comparing the dates as a string
if (![[dateFormatter stringFromDate:currentDate] isEqualToString:[dateFormatter stringFromDate:lastShuffleDate]]) {
NSLog(@"Dates are different...!");
//[self shuffleMyArray];
} else {
NSLog(@"Dates are the same... (midnight has not passed)");
}
// save the time of the last shuffle
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastShuffleDate"];
目前为止,你没有真正的理由去检查时间,但我包含了它以防你好奇。
// remote dateStyle and set timeStyle to check times
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"Current Time: %@", [dateFormatter stringFromDate:currentDate]);
NSLog(@"Saved Time: %@", [dateFormatter stringFromDate:lastShuffleDate]);
NSUserDefaults
?还是只在视图内执行检查方法? - RoshDamunkitimeIntervalSinceNow
返回两个日期之间的秒数。如果用户在23:45打开应用程序,然后在00:01再次打开,只有16分钟(960秒)过去了。因此,<86400秒已经过去,但是已经到了第二天。使用两个日期的NSDayCalendarUnit
进行比较是否有效?(如果它们不相等,则洗牌) - RoshDamunkiNSLocale
更改为 currentLocale
...并且同意 - 据我所知,时间倒流是不可能的...否则我现在就要更换硬盘了....! - RoshDamunki