2011-12-28 3 views
0

私は、notificationを使ってアラームを設定しています。datepickerを使用して特定の日付をuserdefaultsに保存するアラームを設定します設定されたすべてのアラームがテーブルビューに表示されます。このテーブルビューには、編集用のバーボタンと新しいアラーム用のアラームが追加されています。編集ボタンがクリックされると、tableviewが編集モードに変わります特定の行をクリックすると、編集ページが開きます。編集ページには保存ボタンがあります。編集ページは、保存された特定の通知を編集するために使用されます。つまり、私が最初にテーブルビュークラスのdidselectメソッド私のアラームを表示しています私は選択された行のindexpathを渡しました。編集ページが開かれたとき、ユーザーはdatepickerを開くことによって時刻を変更することができます。しかし、問題は、時間が変更されたときに、正しいインデックスで変更された日付配列をどのように置き換えるかです。 これは私が日付ピッカーiphoneでdatepicker時間が選択されているときにuserdefaultsの配列を置き換える方法

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

使用することは、あなたが交換する特定のオブジェクトを取得しているならば、あなたは

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

を使用しなければならない。これは、という特定の日付オブジェクトのインデックスを返します

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で混乱しています:私は書かれるべきか、配列の置き換えを取得するために、インデックス番号がわからない.IF 1 – Rani

+0

の代わりに1つだけあなたのインデックスを置き換える.withoutインデックスあなたは特定のオブジェクトを置き換えることはできません – Narayana

+1

しかし、配列インデックスを知る方法 – Rani

3

...あなたを助けます配列に含まれています。

あなたはまたしてオブジェクトを確認することができます。

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