我有一個計時器的某一個日期我今天部件倒計時。我從我的視圖控制器收集日期,然後在擴展中啓動計時器,然後從三個標籤中顯示日期,分別爲小時,分鐘和秒。在我的應用程序中......這是完美的,但在通知中心並非如此。倒計時加載正確並且效果很好,持續約20秒。之後,它跳過一秒鐘並凍結,然後在大約4秒後重新開始,然後重置整個小部件(一切消失),然後大約2秒鐘後整個小部件重置。這是記憶警告還是什麼?定時器在今天擴展凍結擴建
視圖控制器: mainTextLabel.text = 436496400
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.nicmac.nextgame"];
[sharedDefaults setObject:mainTextLabel.text forKey:@"MyNumberKey"];
[sharedDefaults synchronize];
今天 - 視圖 - 控制器
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDefaultsDidChange:)
name:NSUserDefaultsDidChangeNotification
object:nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.preferredContentSize = CGSizeMake(320, 300);
[self performSelector:@selector(updateNumberLabelText) withObject:nil afterDelay:1.0];
}
- (void)userDefaultsDidChange:(NSNotification *)notification {
[self performSelector:@selector(updateNumberLabelText) withObject:nil afterDelay:1.0];
}
- (void)updateNumberLabelText {
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.nicmac.nextgame"];
NSString *date = [defaults stringForKey:@"MyNumberKey"];
self.numberLabel.text = [NSString stringWithFormat:@"%@", date];
NSString *doubleString = self.numberLabel.text;
double value = [doubleString doubleValue];
destinationDate = [NSDate dateWithTimeIntervalSince1970:value];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateNumberLabelText) userInfo:nil repeats:YES];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
int units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];
[hour setText:[NSString stringWithFormat:@"%ld", (long)[components day] * 24 + (long)[components hour]]];
[minute setText:[NSString stringWithFormat:@"%ld", (long)[components minute]]];
[second setText:[NSString stringWithFormat:@"%ld", (long)[components second]]];
}
謝謝!