1
我正在写一个简单的时钟应用程序,并正在寻找一种方法来调用一个方法,当小时变化(或第二等等的分钟)。如果有人能为我指出正确的方向,我会非常感激它。也许那种调用闹钟方法的实例,我不知道?使用NSDate调用方法?
我正在写一个简单的时钟应用程序,并正在寻找一种方法来调用一个方法,当小时变化(或第二等等的分钟)。如果有人能为我指出正确的方向,我会非常感激它。也许那种调用闹钟方法的实例,我不知道?使用NSDate调用方法?
您可以使用-initWithFireDate:...
安排一个NSTimer,并将启动日期设置为整小时。
如果你正在写精确的闹钟应用到人类秒,这是容易被忽视亚秒级的差异,直接使用+scheduledTimerWithTimeInterval:...
,例如
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateClock)
userInfo:nil
repeats:YES];
...
-(void)updateClock {
NSDate* now = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* dateComps = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
fromDate:now];
NSInteger hourNow = [dateComps hour];
NSInteger minuteNow = [dateComps minute];
NSInteger secondNow = [dateComps second];
// update clock view using 'hourNow', 'minuteNow' and 'secondNow'
if (secondNow == 0) {
// whole minute
if (minuteNow == 0) {
// whole hour
}
// (note: not very reliable since a timer does not need to fire on schedule)
}
我正在使用一种非常类似于此的方法。我需要的是一种在小时内每小时调用一种方法的方法,或者一分钟内每分钟调用一次方法。 – HenryGale 2011-02-06 06:42:03