简体   繁体   中英

how to replace an array of userdefaults when the datepicker time is selected in iphone

i have an application where i am setting an alarm through notification.When the date is selected through datepicker to set the alarm that particular date is saved in userdefaults as an array of dates with key of @"time".All the alarms set is displayed on a tableview.this tableview has barbuttons one for edit and the other for add a new alarm.When the edit button is clicked the tableview changes to edit mode and clicking on any particular row opens the edit page.in the edit page there is a save button .the edit page is used to edit a particular saved notification ie to edit the notification i have initially in the didselect method of my tableview class where i am displaying my alarms i have passed the indexpath of the row selected. When the edit page is opened the user can change the time by opening the datepicker.But the problem is how to replace the array of date with the changed at the properindex when the time is changed . This is my code which is called when i selecting any time from 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"]);
    }   


  }

this is my code to schedule notification which gets scheduled by parsing the dates from the array

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

                }
                }
            }
        }

    }

}

My problem is when i edit a date and click on the save button, that date instead of getting replaced at the proper index value of my array it gets added to my array.What may be the problem.Thanks.

if you are getting the particular object to replace then you must use

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

This will return index of particular date object that your array contains.

You can also check for object with:

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

use like below it will help you...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM