2013-03-19 69 views
5

我有一個ViewController對數組執行隨機洗牌並將文本吐出到Label(在viewDidLoad方法中)。問題是,無論何時我導航到相同的ViewController時,它都會再次執行洗牌,我只需要每天洗牌一次。檢查是否自午夜以來已加載視圖

因此,我需要檢查此ViewController是否在同一天之前(即從午夜開始)被加載,然後我可以將該洗牌放入if語句中。無論應用程序是否打開,我是否可以在午夜安排洗牌?

我已經研究過將布爾值設置爲NSUserDefaults:類似hasLoadedSinceMidnight,但後來無法確定如何在午夜重置布爾值。

+0

這將是一種方式,但你也需要保存你正在「洗牌」並加載這些項目的狀態。 – 2013-03-19 18:49:45

+0

數組對象來自CoreData,因此它們已經被保存。該方法只是將它們加載到一個數組中,並將它們混洗到另一個數組中。 – RoshDamunki 2013-03-19 19:35:30

回答

7

您可以實現的AppDelegate中的significantTimeChange方法:

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

這種方法被稱爲每一個午夜,在顯著時間的變化,例如更改時區。如果您的應用在收到活動時關閉,則在下次打開您的應用時會調用此方法。

更多信息,可以查看here

的另一種方法做同樣的事情,你的視圖控制器內,而不是在AppDelegate的是添加:

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

,然後你可以執行你的該ViewController的-(void)performAction;方法中的隨機操作。

+1

+1或更好:訂閱['UIApplicationSignificantTimeChangeNotification'](http://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006728-CH3-SW44)在視圖控制器 – vikingosegundo 2013-03-21 01:21:11

+0

這是真的。我補充說。 – Jsdodgers 2013-03-21 01:25:32

+0

+1比我建議的要好得多! – rog 2013-03-21 01:29:40

0

取代存儲BOOL,您可以存儲上次隨機播放的日期/時間(NSDate)。

然後通過比較存儲日期和viewDidAppear中的當前日期,檢查自上次洗牌後是否已經過午夜。

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


UPDATE:

按照要求,這裏是一些示例代碼。無可否認,這可能是更好的解決方案,但我相信這段代碼會幫助你解決問題。這樣做是檢查是否有使用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]); 
+0

有趣的 - 所以當視圖加載並保存到'NSUserDefaults'時創建一個時間戳?或者只是在視圖中執行檢查方法? – RoshDamunki 2013-03-19 19:39:03

+0

當您洗牌時,將時間存儲在NSUserDefaults中。在viewDidAppear(可能是您需要的更好的選項)中,檢查當前時間與NSUserDefaults中保存的時間,並確定午夜是否已過。如果有,重新洗牌並用當前時間更新NSUserDefaults以反映最後一次洗牌。 – rog 2013-03-19 19:42:37

+0

你可以給我一些示例代碼。我試圖讓這個工作,但似乎無法。 'timeIntervalSinceNow'返回兩個日期之間的秒數。如果用戶在23點45分打開應用程序,然後在00點01分再打開應用程序,只有16分鐘(960秒)已經過去。因此,<86400s已經通過,但是這是第二天。這兩個日期的「NSDayCalendarUnit」會比較嗎? (如果他們不==然後洗牌) – RoshDamunki 2013-03-20 09:43:59