2011-12-28 15 views
0

選擇了日期選擇時間我有我在哪裏設置通過notification.When警報的時間通過日期選擇器選擇來設置特定的日期被保存在userdefaults作爲警報的應用程序如何更換userdefaults的陣列日期與@「時間」。所有被顯示在tableview.this實現代碼如下設置的報警鍵陣列具有barbuttons一個用於編輯,另一個用於添加新alarm.When編輯按鈕被點擊的實現代碼如下變更爲編輯模式並點擊任何特定行打開編輯page.in編輯頁面有一個保存按鈕.the編輯頁面用於編輯特定保存的通知,即編輯通知我有最初在我的tableview類的didselect方法,其中i我正在顯示我的警報,我已通過所選行的索引路徑。當打開編輯頁面時,用戶可以通過打開日期選擇器來更改時間。但問題是如何在時間更改時使用適當索引處更改的日期數組替換。 這是我的代碼時,我從日期選擇器當在iphone

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

       } 
       } 
      } 
     } 

    } 

} 

我的問題是,當解析日期安排通知我編輯一個日期,然後單擊保存按鈕,該日期,而不是被取代我的數組的正確的索引值它將被添加到我的數組。這可能是問題。謝謝。

回答

0

使用如下它會幫助你...

NSArray *arry_img = [[NSArray alloc] initWithObjects:@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",@"[email protected]",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"]); 
+0

納拉亞納,但我正在逐漸由replaceObjectAtIndex困惑:1。如果我不知道索引號被替換數組的那麼應該怎麼寫 – Rani 2011-12-28 10:46:21

+0

代替1,你只需更換你的索引。沒有指數不要去碰更換特定對象 – Narayana 2011-12-28 10:52:39

+1

但如何知道數組索引 – Rani 2011-12-28 10:55:17

3

如果你所得到的特定對象來代替,則必須使用

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

這將返回特定日期對象的索引你的數組包含。

您還可以檢查對象有:

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