2012-08-16 45 views
0

我是iPhone應用程序開發新手。現在我正在開發iPhone的鬧鐘應用程序。在這個應用程序中,我從UIDataPicker中選擇數據。然後我應用於觸發報警按鈕操作的NSLocalNotification。這是第一次工作。然後再次點擊那個按鈕我再次工作,但時間也一樣。這是錯誤的工作。如何設置報警應用的NSTimer?

在這裏,我想我需要我們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

設定按鈕連接到運行在其使用 UILocalNotification類 時刻表的通知的 視圖控制器 方法稱爲 scheduleNotification。代碼 看起來如下:

(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]; 
} 
}