检查是否自午夜以来已加载视图

我有一个ViewController对数组执行随机洗牌,并将文本吐出到Label(在viewDidLoad方法中)。 问题是,每当我导航到相同的ViewController它再次执行洗牌,我只需要它每天洗牌一次。

所以我需要检查这个ViewController是否在同一天(也就是从午夜开始)之前被加载过,然后我可以把这个shuffle放到一个if语句中。 无论应用程序是否打开,我是否可以在午夜安排洗牌?

我已经考虑设置一个布尔到NSUserDefaults :类似hasLoadedSinceMidnight但不能解决如何在午夜重置布尔值。

你可以实现AppDelegate的significantTimeChange方法:

 -(void)applicationSignificantTimeChange:(UIApplication *)application { //tell your view to shuffle } 

这种方法被称为每个午夜,并在时间的重大变化,如时区变化。 如果您的应用程序在收到事件时closures,则在下次打开应用程序时会调用此方法。

更多信息可以在这里查看

在你的ViewController而不是AppDelegate中做同样的事情的另一种方法是添加:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAction) name:UIApplicationSignificantTimeChangeNotification object:nil]; 

然后你可以在-(void)performAction;执行你的shuffle操作-(void)performAction; 那个ViewController的方法。

可以存储最后一个shuffle的date/时间( NSDate ),而不是存储BOOL

然后通过比较存储的date和viewDidAppear的当前date,检查自上次洗牌后是否已经过午夜。

请参阅NSTime文档: http : //developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

NSDateFormatter文档: https : //developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003643


更新:

根据要求,这里是一些示例代码。 无可否认,这可能是更好的解决scheme,但我相信这段代码将帮助你解决问题。 这样做是检查是否有使用NSUserDefaults保存date,然后与当前date进行比较。 如果date不匹配,洗牌数组,然后保存当前date(再次使用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]);