0

我是 iPhone 应用程序开发的新手。现在我正在为 iPhone 开发警报应用程序。在这个应用程序中,我从 UIDataPicker 中选择了数据。然后我在警报按钮动作中应用了 NSLocalNotification 触发。这是第一次工作。然后第二次再次单击该按钮,我也再次工作,但时间也相同。这是错误的工作。

在这里我想我需要我们的 NSTimer。我不知道如何使用 NSTimer,而且它正在工作的后台应用程序也如何设置这个计时器。

遵循开发的警报通知代码。

        - (void) saveButtonAction:(id)sender {
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            Resource *resourceLoader = [[Resource alloc] init];

            NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];
            NSString *musicName;
            NSDate *selectedDateTime = [[NSDate alloc]init];
            if (prefDic && [prefDic objectForKey:@"alarmtime"]) {
                //firstVal_textField.text = [prefDic objectForKey:@"value1"];
                NSLog(@"saravanan %@",[prefDic objectForKey:@"alarmtime"]);
                selectedDateTime = [prefDic objectForKey:@"alarmtime"];
                NSLog(@"saravanan periyasamy %@", selectedDateTime);

                musicName = [NSString stringWithFormat:@"%@%@",[prefDic                objectForKey:@"alarmmusic"],@".wav" ];

            } 
            UILocalNotification *notif = [[cls alloc] init];
            //notif.fireDate = [datePicker date];
            notif.fireDate = selectedDateTime;
            notif.timeZone = [NSTimeZone defaultTimeZone];

            notif.alertBody = @"Alarm";
            notif.alertAction = @"Show me";
            //notif.repeatInterval = 0;
            //notif.soundName = UILocalNotificationDefaultSoundName;
            notif.soundName = musicName;
            notif.applicationIconBadgeNumber = 1;
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"saravanan"
                                                                    forKey:kRemindMeNotificationDataKey];
            notif.userInfo = userDict;
            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            [notif release];
}
}
}
4

1 回答 1

1

设置按钮被连接起来以在视图控制器中运行一个名为 scheduleNotification 的方法,该方法使用 UILocalNotification 类来安排通知。代码如下所示:

   (void )scheduleNotification
  {
 [reminderText resignFirstResponder];
 [[ UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(@ "UILocalNotification" );
  if (cls != nil)
    {
 UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [datePicker date];
    notif.timeZone = [ NSTimeZone defaultTimeZone];
    notif.alertBody = @ "Did you forget something?" ;
    notif.alertAction = @ "Show me" ;
    notif.soundName = UILocalNotificationDefaultSoundName ;
       notif.applicationIconBadgeNumber = 1 ;
 NSDictionary *userDict = [       NSDictionary dictionaryWithObject:reminderText.text
          forKey:kRemindMeNotificationDataKey];
    notif.userInfo = userDict;
      [[ UIApplication sharedApplication] scheduleLocalNotification:notif];
    [notif release];
}
}
于 2012-08-16T17:07:24.383 回答