2013-03-23 47 views
1

我目前正在處理本地通知。本地通知被觸發導致錯誤

- (void)viewDidAppear:(BOOL)animated { 

NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; 
[components setHour:uhrzeit]; 
_date_today = [calendar dateFromComponents:components]; 


if (akt_tag != proj_tag) { 
    _date_final = [NSDate dateWithTimeInterval:(86400*(proj_tag - akt_tag)) sinceDate:_date_today]; 
    } 
else { 
    _date_final = [NSDate dateWithTimeInterval:0 sinceDate:_date_today]; 
    } 



    NSLog(@"%@", _date_final); 

}

- (IBAction)erinnerung:(id)sender { 

UILocalNotification *notification = [[UILocalNotification alloc] init]; 
notification.alertBody = @"It's Notific Time!"; 
notification.soundName = @"benachrichtigung.mp3"; 
notification.timeZone = [NSTimeZone defaultTimeZone]; 
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:[_date_final timeIntervalSinceDate:[NSDate date]]]; 
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
[notification release]; 

}

但我發現了這個錯誤(更精確:在notfication.fireDate):

2013-03-23 12:50: 21.303 Notific [4593:907] - [NSISRestrictedToNonNegativeMarkerVariableToBeMinimized timeIntervalSinceDate:]:無法識別的選擇器發送到實例0x1dd514b0 2013年3月23日12:50:21.305 Notific [4593:907] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [NSISRestrictedToNonNegativeMarkerVariableToBeMinimized timeIntervalSinceDate:]:無法識別的選擇發送到實例0x1dd514b0' *第一擲調用堆棧: (0x337723e7 0x3b463963 0x33775f31 0x3377464d 0x336cc208 0x56b33 0x3566c087 0x3566c03b 0x3566c015 0x3566b8cb 0x3566bdb9 0x355945f9 0x355818e1 0x355811ef 0x372985f7 0x37298227 0x337473e7 0x3374738b 0x3374620f 0x336b923d 0x336b90c9 0x3729733b 0x355d52b9 0x4ae5f 0x3b890b20) 的libC++ abi.dylib:終止叫做拋出異常

你能告訴我這裏有什麼問題嗎?

回答

0

您不保留_date_final。因此當您試圖在功能erinnerung中訪問它時,_date_final的對象無效。

通過belw方式更改代碼:

if (akt_tag != proj_tag) { 
    _date_final = [NSDate dateWithTimeInterval:(86400*(proj_tag - akt_tag)) sinceDate:_date_today]; 
    } 
else { 
    _date_final = [NSDate dateWithTimeInterval:0 sinceDate:_date_today]; 
    } 

[_date_final retain]; 
+0

非常感謝您! – 2013-03-23 12:26:47

+0

@ThomasTobsiSeidl如果有幫助,你應該接受答案。 – Apurv 2013-03-23 12:30:02

+0

不得不等待3分鐘,直到我可以接受它;) – 2013-03-23 12:32:01

0
UILocalNotification *notif = [[UILocalNotification alloc] init]; 

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *dateComp = [[NSDateComponents alloc] init]; 

if (akt_tag != proj_tag) { 
    // after One Day 
    [dateComp setDay:1]; 
} 

NSLog(@"Year: %i, Month: %i, Day: %i, Time:%i:%i\n",[dateComp year], [dateComp month], 
     [dateComp day], [dateComp hour], [dateComp minute]); 

NSDate *fireDate = [gregorian dateByAddingComponents:dateComp toDate:[NSDate date] options:0]; 

notif.fireDate = fireDate; 
notif.timeZone = [NSTimeZone defaultTimeZone]; 

notif.alertBody = @"Your NOtification Message"; 

[[UIApplication sharedApplication] scheduleLocalNotification:notif];