0

我有一个表单,用户填写所有信息,例如名称、事件名称、提醒日期等。所以我要做的是根据这样的键设置通知

- (void)scheduleNotificationForNotificationID:(NSString *)notificationID1:(NSString *)notificationID2:(NSString *)notificationKey 
{ 
    //Set notification after confirmation of saved data 

    Class cls = NSClassFromString(@"UILocalNotification"); 
    reminderNotification = [[cls alloc] init]; 
    if (cls != nil) 
    { 
       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease]; 
       [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 
       NSDate *notificationDate = [dateFormat dateFromString:textField2.text]; 
       reminderNotification.fireDate = notificationDate; 
       reminderNotification.timeZone = [NSTimeZone defaultTimeZone]; 
       NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
       reminderNotification.alertBody = reminderText; 
       reminderNotification.alertAction = @"View"; 
       reminderNotification.soundName = @"apple_ring.mp3"; 
       reminderNotification.applicationIconBadgeNumber = 1; 
       //Use name and event name as keys
       NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationID1,@"Name",notificationID2,@"Event",notificationKey,@"Date",nil];  
       reminderNotification.userInfo = infoDict; 
       [[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification]; 
    } 
}

textField 保存名称,textField1 保存事件名称

所以我有一个保存按钮操作来保存每个提醒,如果按钮标题为“保存”,它将保存提醒并插入新记录,如果是“完成”,它将编辑提醒详细信息并记录将得到更新。

现在通知的问题不是不正确的触发或不触发等。问题在于通知警报正文,这是接收本地通知的代码:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo objectForKey:kReminder];
    [self.addViewController performSelector:@selector(showReminder:) withObject:reminderText afterDelay:1];
}

所以这是通知警报正文的操作

//Notification alert
- (void)showReminder:(NSString *)text
{
    self.reminderAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:reminderNotification.alertBody delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
    UIImage *image= [UIImage imageNamed:@"icon@2x.png"];
    [imageView setImage:image];

    [reminderAlert addSubview:imageView];
    [imageView release];

    [reminderAlert show];
    [reminderAlert release];
}

现在,在我编辑提醒/记录详细信息后,以前的通知会以任何方式被取消,但我的问题是警报正文与通知警报正文不同,即通知警报正文得到更新,但不是警报正文,即根据名称和事件名称例如:

“史蒂夫·乔布斯 7 月 3 日生日”,如果这是保存时的初始事件,并且用户更新/更改为“史蒂夫·乔布斯 7 月 3 日生日”

警报正文保存先前的事件名称值,其中通知警报正文保存正确且正确更新的新提醒详细信息。如果标题已保存,我尝试分配一个警报正文,如果标题完成,则分配另一个值,我的意思是我拿了几个字符串来保存名称和事件名称的值,用于保存和完成操作,全局声明字符串,在我在完成操作中分配提醒正文之前尝试使字符串值为零等......没有任何效果!

因此,我自己分配了提醒通知警报正文。即使这样也不起作用:(

我哪里错了……???

请帮助我,并提前感谢大家:)

4

1 回答 1

1

我找到了将文本分配给提醒警报正文的适当解决方案,即我们基于三个键保存通知:

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationID1,@"Name",notificationID2,@"Event",notificationKey,@"Date",nil];  

我们可以使用这些键为警报视图分配文本/正文,即:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    application.applicationIconBadgeNumber = 0;
    NSString *name = [notification.userInfo objectForKey:@"Name"];
    NSString *event = [notification.userInfo objectForKey:@"Event"];

    //Get the date of event and convert it to required format...
    NSString *date = [notification.userInfo objectForKey:@"Date"];
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease]; 
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
    NSDate *dateVal = [dateFormat dateFromString:date]; 
    [dateFormat setDateFormat:@"MMMM dd"]; 
    NSString *eventDate = [dateFormat stringFromDate:dateVal];

    NSString *reminderText = [NSString stringWithFormat:@"%@'s %@ on %@", name, event, eventDate];
    [self.addViewController performSelector:@selector(showReminder:) withObject:reminderText afterDelay:1];
}

我们不必担心 showReminderAction 中的任何文本分配,即:

- (void)showReminder:(NSString *)text
{
    self.reminderAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:text delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [reminderAlert show];
    [reminderAlert release];
}

这应该会让事情顺利进行,希望这对某人有所帮助,谢谢:)

于 2012-07-03T10:03:36.763 回答