0

UILocalNotificationに収納できますNSUserDefaultsか?

値を取得しようとしましたが、常に null が返されます。

値を取得しようとするメソッドは次のとおりです

-(IBAction)doneClicked:(id)sender
{
    NSUserDefaults *prefx = [NSUserDefaults standardUserDefaults];
    UILocalNotification *oldNotifObj = [prefx objectForKey:@"NotificationObject"];
    NSLog(@"oldNotifObj = %@",oldNotifObj);

    NSLog(@"enable notification");
    if (oldNotifObj == nil)
    {
        [self addNotification];
        NSLog(@"add a new one");
    }
    else
    {
        //if notification exist, remove old one, and add new one.
        [self removeNotification:oldNotifObj];
        [prefx setObject:nil forKey:@"NotificationObject"];
        [self addNotification];
        NSLog(@"remove old notification and add a new one");
    }
}

そしてここにaddNotification方法があります

-(void)addNotification
{
    //set the notification
    UILocalNotification *localNotif = [[UILocalNotification alloc]init];
    localNotif.fireDate = self.timePicker.date;
    localNotif.repeatInterval = NSDayCalendarUnit;
    localNotif.alertBody = @"Hello world!";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication]scheduleLocalNotification:localNotif];

    NSLog(@"localNotif = %@",localNotif);

    //saving notification to prefs    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:localNotif forKey:@"NotificationObject"];
    [prefs synchronize];
}

oldNotifObj常に null を返します。

4

2 に答える 2

5

はい、nullを返します。プロパティリストファイルを使用してデフォルトのNSUserDefaults読み取りと書き込みを行うため、プロパティリストタイプのみを管理できNSStringますNSNumber:、、、、、、。NSDataNSDateNSArrayNSDictionary

UILocalNotification全体として、にを格納することはできませんNSUserDefaults

于 2012-07-05T05:53:06.017 に答える
1

値を手動でNSDictionaryに変換し、それをuserdefaultsに追加してみてください。それはこれを解決します。つまり、etcなどのアイテムlocalNotif.fireDate localNotif.repeatInterval localNotif.alertBody localNotif.soundNameを文字列オブジェクトに変換してNSDictionaryに設定できます。したがって、それらをuserdefaultsに保存できます。`

于 2012-07-05T05:54:56.857 に答える