如何设置NSTimer报警应用程序?

时间:2012-08-16 15:00:35

标签: iphone nstimer alarm

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

在这里,我认为我需要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];
}
}
}

1 个答案:

答案 0 :(得分:1)

设置按钮已连线运行a 方法叫做 scheduleNotification in 查看控制器使用 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];
}
}