2012-05-28 161 views
4

我想設置一週中選定日期的鬧鐘。可能是星期一(星期一,星期三,星期四,星期四或星期五),週末(星期六,星期日)或每天。如何在iPhone中爲選定的日期設置鬧鐘?

我該如何使用本地通知來做到這一點?

+0

檢查[this](http://www.icodeblog.com/2010/07/29/iphone-programming-tutorial-local-notifications/)和[Local Notification Tutorial](http://reecon.wordpress。 COM/2010/11/12 /設定-A-提醒-使用-uilocalnotification式-IOS -4-目標c /)。還請檢查此[Apple文檔](https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html)。 –

回答

2

如果你有,你必須每天解僱通知的時候,你應該這樣做

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *components = [[NSDateComponents alloc] init]; 
    for (NSMutableArray * arrDay in self.namaztimes) { 
     NSLog(@"Alarm Array: %@", arrDay); 
     int count=[arrDay count]; 
     if(!count){ 
      continue; 
     } 

     int day =0; 
     int month=0; 
     int year=0; 
     int hour =0; 
     int minutes=0; 

     // NSArray *arrDates=[[NSMutableArray alloc] initWithCapacity:1]; 
     for (int i=0;i<3;i++) { 
      NSString * dayTime=[arrDay objectAtIndex:i ]; 
      if (i==0) { 
       day = [dayTime intValue];  
      }else if(i==1){ 
       month = [dayTime intValue];      
      }else if(i==2){ 
       year = [dayTime intValue];  

      } 
     } 
     for (int i=3;i<count;i++) { 
      NSString * dayTime=[arrDay objectAtIndex:i ]; 
      hour = [[dayTime substringToIndex:2] intValue]; 
      minutes = [[dayTime substringFromIndex:3] intValue]; 

      [components setDay:day]; 
      [components setMonth:month]; 
      [components setYear:year]; 
      [components setMinute:minutes]; 
      [components setHour:hour]; 


      NSDate *myNewDate = [calendar dateFromComponents:components]; 

      [self scheduleNotificationForDate:myNewDate]; 

     } 
    } 

    [components release]; 
    [calendar release]; 

然後從這裏它會連接到主通知燒製法
[self scheduleNotificationForDate:myNewDate];

-(void) scheduleNotificationForDate: (NSDate*)date { 
    /* Here we cancel all previously scheduled notifications */ 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
    localNotification.fireDate = date; 
    NSLog(@"Notification will be shown on: %@ ",localNotification.fireDate); 

    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.alertBody = @"Your Notification Text"; //[NSString stringWithFormat:@"%@",date]; 
    localNotification.alertAction = NSLocalizedString(@"View details", nil); 

    /* Here we set notification sound and badge on the app's icon "-1" 
    means that number indicator on the badge will be decreased by one 
    - so there will be no badge on the icon */ 

    localNotification.repeatInterval = NSDayCalendarUnit; 
    localNotification.soundName = UILocalNotificationDefaultSoundName; 
    localNotification.applicationIconBadgeNumber = -1; 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 
+0

嗨sree charan,謝謝我只想爲選定的日子設置鬧鐘。我該怎麼做? – Ann

+0

自定義我的代碼,我已經給所有的日曆事件,日,月,年,小時等int值,只是動態地發送你的值到這個方法 – Charan

+0

這意味着,是否有可能從數組中提供日期值和什麼是self.namaztimes?(是這個鬧鐘時間。) – Ann

1

我知道這個問題在很久以前就被問到了,我加了這個,因爲它對一些人會有幫助。

如果您想通知選定日期,請按照此tutorial。這將有所幫助。

相關問題