2013-03-26 53 views
-1

嗨,我想在特定日期做些什麼,此刻我只是記錄一些隨機的東西,但日誌直接顯示在應用程序啓動時,而不是在我的日期已經確定。這是我的代碼。Xcode:在特定日期做些什麼

-(void)theMagicDate{ 

    NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]]; 

    [nextComp setYear:2013]; 
    [nextComp setMonth:3]; 
    [nextComp setDay:26]; 
    [nextComp setHour:10]; 
    [nextComp setMinute:05]; 

    UIDatePicker *nextDay = [[UIDatePicker alloc]init]; 
    [nextDay setDate:[nextCal dateFromComponents:nextComp]]; 

    if(nextDay.date){ 
     NSLog(@"Doing the stuff on the date"); 
    } 
} 

,我調用這個函數從viewDidLoad中

回答

3

那麼你正在做一些錯誤的假設:

首先if(nextDay.date){會永遠是真的。因爲它只會檢查是否有任何事物被分配到財產日期。既然你給該屬性分配了一個日期,它將是真實的。

其次,UIDatePicker是一個用戶界面(UI)組件,它允許用戶選擇一個日期。 如果你想檢查你創建的組件已粘貼的日期,現在或將來你將不得不在NSDate上使用方法。

像這樣:

-(void)theMagicDate{ 

NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]]; 

[nextComp setYear:2013]; 
[nextComp setMonth:3]; 
[nextComp setDay:26]; 
[nextComp setHour:10]; 
[nextComp setMinute:05]; 

NSDate *dateToCheck = [nextCal dateFromComponents:nextComp]; 
NSDate *now = [NSDate date]; 


switch ([now compare:dateToCheck]) { 
    case NSOrderedAscending: 
     NSLog(@"Date in the future"); 
     break; 

    case NSOrderedDescending: 
     NSLog(@"Date in the past"); 
     break; 

    case NSOrderedSame: 
     NSLog(@"Date is now"); 
     break; 
} 


} 
+0

即時通訊嘗試將此日期和時間設置爲將來1分鐘,但我只收到字符串@「將來的日期」,但它不知道該日期的時間通過。 – Mikzstone 2013-03-26 09:23:55

+0

如果您在將來檢查日期時將日期設置爲一分鐘。如果我運行上面的例子,它會給控制檯日誌說:'過去的日期' – rckoenes 2013-03-26 09:26:26

+0

好吧,所以我得到它的工作,但只有當重新啓動手機。即將發生的事情是,這種觀點應該是積極和正在運行的,並且仍然會啓動將在特定日期和時間發生的事情。 – Mikzstone 2013-03-26 09:36:51

0
if(nextDay.date){ 
    NSLog(@"Doing the stuff on the date"); 
} 

始終是真實的。

您需要將當前日期與從選取器或任何位置選擇的日期進行比較。您需要保存,以後日期userdefaults或plist中等等。theMagicDate方法閱讀它,兩個日期比較,然後進入NSLog(@"Doing the stuff on the date");

-(void)theMagicDate{ 

    NSCalendar *nextCal = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *nextComp = [nextCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]]; 

    [nextComp setYear:2013]; 
    [nextComp setMonth:3]; 
    [nextComp setDay:26]; 
    [nextComp setHour:10]; 
    [nextComp setMinute:05]; 

    UIDatePicker *nextDay = [[UIDatePicker alloc]init]; 
    [nextDay setDate:[nextCal dateFromComponents:nextComp]]; 


    //read from plist etc 
    NSDate *readDate=... 


    if([readDate compare:nextDay.date]==NSOrderedSame){ 
     NSLog(@"Doing the stuff on the date"); 
    } 
} 
+0

+1很好的答案。 – Girish 2013-03-26 09:10:22