我有一个应用程序,我通过通知设置警报。当通过datepicker选择日期时,将特定日期保存在userdefaults中的警报设置为日期数组,键为@" time&#34 ;。所有报警集都显示在tableview上。这个tableview有一个barbuttons一个用于编辑,另一个用于添加一个新报警。当点击编辑按钮时,tableview变为编辑模式并点击任何特定行打开编辑页面在编辑页面上有一个保存按钮。编辑页面用于编辑特定保存的通知,即编辑我最初在我的tableview类的didselect方法中的通知,其中我显示我已经通过索引路径的警报路径选中的行。打开编辑页面时,用户可以通过打开datepicker来更改时间。但问题是如何在更改时间时将date数组替换为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];
}
}
}
}
}
}
我的问题是当我编辑日期并单击保存按钮时,该日期而不是在我的数组的正确索引值处被替换它会被添加到我的数组中。可能是问题。谢谢。
答案 0 :(得分:3)
如果要获取要替换的特定对象,则必须使用
[arr indexOfObject:<#(id)#>]
这将返回数组包含的特定日期对象的索引。
您还可以使用以下方法检查对象:
if([arr containsObject:<#(id)#>])
{
int index = [arr indexOfObject:<#(id)#>];
}
答案 1 :(得分:0)
使用如下,它会帮助你...
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"]);