2013-03-26 57 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