如何在iphone中选择日期选择器时间时替换用户默认值数组



我有一个应用程序,我通过通知设置警报。当通过datepicker选择日期来设置闹钟时,该特定日期将保存在用户默认值中,作为日期数组,键为@"time"。所有设置的告警都显示在一个表视图中。这个tableview有barbutton一个用于编辑另一个用于添加新警报。当编辑按钮被点击时,tableview改变到编辑模式,点击任何特定的行打开编辑页面。在编辑页面有一个保存按钮。编辑页面是用来编辑一个特定的保存通知,即编辑通知我最初在我的tableview类的didselect方法中,我在那里显示我的警报,我已经通过了所选行的indexpath。当编辑页面打开时,用户可以通过打开日期选择器来更改时间。但问题是如何替换日期的数组与更改在properindex时,时间发生了变化。这是我的代码当我从datepicker

中选择任何时间时调用
-(void)convertDueDateFormat{
    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
    app.timerdate = [self.datePicker date];
    NSLog(@"earier picker date:%@",app.timerdate);
    [app.dateFormatter setDateFormat:@"hh:mm a"];
    NSDate *today = [NSDate date];
    app.alarmtime = [app.dateFormatter stringFromDate:app.timerdate];
    NSDate *alarmdate = [app.dateFormatter dateFromString:app.alarmtime];
    // this is used to compare the timerdate is lesser than the current time. If yes than it will add 7 days to it. 
    if ([app.timerdate compare:today] == NSOrderedAscending)
    {
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];  
        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];   
        [dateComponents setDay:7];   
        app.timerdate = [gregorian dateByAddingComponents:dateComponents toDate:[self.datePicker date] options:0];  
        NSLog(@"check picker date:%@",app.timerdate);
        [dateComponents release];  
        [gregorian release];
    }

    NSLog(@"picker date:%@",app.timerdate);

    if ([[NSUserDefaults standardUserDefaults] arrayForKey:@"time"]==nil)
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        array =  [[NSMutableArray alloc] initWithArray:[userDefaults arrayForKey:@"time"]];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"time"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
    }
    else
    {
        array = [[NSUserDefaults standardUserDefaults] objectForKey:@"time"];
        app.array_dates = [[NSMutableArray alloc] init];
        for(int i =0;i<[array count];i++)
        {
            [app.array_dates addObject:[array objectAtIndex:i]];
        }
        if (app.timerdate) {
            [app.array_dates addObject:app.timerdate];
        }
        [[NSUserDefaults standardUserDefaults] setObject:app.array_dates forKey:@"time"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]);
    }   

  }

这是我调度通知的代码,它通过解析数组

中的日期来调度
-(void)scheduleNotification
{
    timepicker = [[TTimePickerController alloc]init];
    double double_date = (double)[[NSDate date] timeIntervalSince1970];     
       NSDate *now= [NSDate dateWithTimeIntervalSince1970:double_date];
    selectedWeek = [[NSUserDefaults standardUserDefaults]objectForKey:@"weekday"];
    NSMutableArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
    NSInteger repeatCount = 7;
    NSDate *today = [NSDate date];
    NSDate *date1;
    NSString *Stringdate;
    calendar = [NSCalendar currentCalendar];
    NSDateFormatter *format = [[[NSDateFormatter alloc]init]autorelease];
    format.dateFormat = @"yyyy-MM-dd hh:mm";
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    dateFormat.dateFormat = @"yyyy-MM-dd";
    NSDateFormatter *timeFormat = [[[NSDateFormatter alloc]init]autorelease];
    timeFormat.dateFormat = @"hh:mm";
    for (NSDate *date in array)
    {
        if ([date isEqualToDate:now]) 
        {
            itemDate = date;
            NSLog(@"%@",itemDate);
        } 
        else if([date earlierDate:now]){
            itemDate = date;
        }
        else if([date laterDate:now])
        {
            itemDate = date;
        }
    }
    if (array_notif == nil)
    {   array_notif = [[NSMutableArray alloc]init];
        UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 
    {   BOOL idGenereated=FALSE;
        for (int i = 0; i < repeatCount; i++) 
        {   int minutesBefore;
            date1 = [today dateByAddingTimeInterval:i*24*60*60];
            dateComponents  =[calendar components:NSWeekdayCalendarUnit fromDate:date1];
            if ([selectedWeek containsObject:[NSNumber numberWithInteger:dateComponents.weekday - 1]]) {
                Stringdate = [NSString stringWithFormat:@"%@ %@",[dateFormat stringFromDate:date1 ],[timeFormat stringFromDate:itemDate]];
                    notification.fireDate =itemDate;
                    NSLog(@"%@",notification.fireDate);
                    notification.timeZone = [NSTimeZone defaultTimeZone];
                    notification.alertBody = app.messagetext;
                    notification.alertAction = @"View";
                    notification.soundName = app.newsong;
                    NSLog(@"notification sound name:%@",notification.soundName);
                    notification.applicationIconBadgeNumber=1;
                    notification.repeatInterval = 0;
                    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
                    NSString *str_alarmbody = notification.alertBody; 
                    NSString *str_id = [[str_alarmbody componentsSeparatedByString:@" "] objectAtIndex:1];
                    NSLog(@"Notif userinfo:%@",str_id);
                    NSLog(@"%@",notification.alertBody);
                    if(!idGenereated)
                    {
                        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
                        NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];
                        NSLog(@"%d",timeStampObj);
                        am.AlarmID =(int)timeStampObj;
                        NSLog(@"%@",am.AlarmID);
                        idGenereated=TRUE;
                    }
                    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
                    [app.newtest setObject:[NSNumber numberWithInteger:am.AlarmID]forKey:@"idtemp"];
                    [app.newtest setObject:app.timerdate forKey:@"iddate"];     
                    notification.userInfo = app.newtest;
                    NSLog(@"notif userinfo:%@",notification.userInfo);
                [[UIApplication sharedApplication]scheduleLocalNotification:notification];
                }
                }
            }
        }
    }
}

我的问题是当我编辑日期并单击保存按钮时,该日期而不是在我的数组的适当索引值上被替换,它被添加到我的数组中。

如果要替换特定对象,则必须使用

[arr indexOfObject:<#(id)#>]

返回数组中包含的特定日期对象的索引。

你也可以用

检查对象:
 if([arr containsObject:<#(id)#>])
{
    int index = [arr indexOfObject:<#(id)#>];
}

在下面使用like会帮助你…

NSArray *arry_img = [[NSArray alloc] initWithObjects:@"frame_18@3x.png",@"frame_17@3x.png",@"frame_1222@3x.png",@"frame_10@3x.png",@"frame_3@3x.png",@"frame_4@3x.png",@"frame_4@3x.png",@"frame_1@3x.png",@"frame_4@3x.png",@"frame_4@3x.png",nil];
        [[NSUserDefaults standardUserDefaults] setObject:arry_img forKey:@"categoryArray"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"before %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"categoryArray"]);
        NSMutableArray *array_cat =[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"categoryArray"]] ;
        [array_cat replaceObjectAtIndex:1 withObject:@"1.png"];
        [[NSUserDefaults standardUserDefaults] setObject:array_cat forKey:@"categoryArray"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        NSLog(@"after:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"categoryArray"]);

最新更新