2012-02-09 70 views
2

我想在這個時間調用一個方法:00:01 AM這裏是我的代碼,但我找不到問題爲什麼這個代碼不會調用我的方法。iOS:在特定時間調用方法

- (BOOL)date:(NSDate *)date hour:(NSInteger)h minute:(NSInteger)m { 

    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:currentCalendar]; 

    NSDateComponents *componets = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]]; 
    if ([componets hour ] == h && [componets minute] == m) { 

     return YES; 
    } 
    return NO; 
} 



- (void)updateNewDate { 

    if ([self date:[NSDate date] hour:4 minute:49]) { 

     label.text = @"oyooy"; 
    } 
} 

- (void)viewDidLoad { 

[self updateNewDate]; 
} 

如果您有其他方法請告訴我。

+0

你確定你的應用程序在上午12點01分運行。即應用程序在前臺運行? – Futur 2012-02-09 13:42:01

回答

2

其實你的代碼工作正常,但它需要刷新...例如,你可以使用NSTimer 刷新方法:

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(updateNewDate) userInfo:nil repeats:YES]; 
2

本地通知是要走的路....

編輯... 第二種方式是獲取當前時間,每當應用程序啓動並找到時間,直到下一個午夜,然後啓動一個定時器火在那段時間之後。 希望這有助於。

+0

不,我不想用本地notif – 2012-02-09 13:33:58

+0

@ Mc.Lover查看編輯 – 2012-02-09 13:38:05

+0

這可以在後臺完成嗎?沒有把應用程序前景? – User3 2014-01-21 13:34:17

相關問題