【问题标题】:Local notification firedate causes error本地通知触发导致错误
【发布时间】:2013-03-23 12:15:28
【问题描述】:

我目前正在处理本地通知。

- (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 通知 [4593:907] -[NSISRestrictedToNonNegativeMarkerVariableToBeMinimized timeIntervalSinceDate:]:无法识别的选择器发送到实例 0x1dd514b0 2013-03-23 12:50:21.305 通知 [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:终止调用抛出异常

你能告诉我这里有什么问题吗?

【问题讨论】:

    标签: ios objective-c uilocalnotification


    【解决方案1】:

    您没有保留_date_final。所以当你试图在函数erinnerung中访问它时,_date_final的对象是无效的。

    通过以下方式更改您的代码:

    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];
    

    【讨论】:

    • @ThomasTobsiSeidl 如果有帮助,您应该接受答案。
    【解决方案2】:
    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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-10
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多