在报警时测试正确的时间

时间:2012-03-24 21:14:46

标签: iphone xcode alarm

抱歉谷歌翻译犯了错误,很多错误,

我想在Xcode中发出警报,但是我遇到了一个问题,如果我使用带有日期和时间的选择器视图,那么每件事都可以正常工作,但是如果我只用时间切换一个选择器视图,就不可能设置警报,因为如果我在之前的某个时间设置闹钟,请立即响铃,

如何在不使用带日期的选择器视图的情况下解决问题?

这是设置闹钟的操作。

PS抱歉预览消息也许我的英语比谷歌翻译更好......

  // Set Allarm Uno

-(IBAction)setAlarmUno:(id)sender{

NSLog(@"In Alarm Date");
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
NSLog(@"About to Set Date");
AlarmDateUno = [alarmDatePickerUno date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
isAlarmUnoOn = 1;


// Setta la label della data con l'orario

NSString *notifyTime = [[NSString alloc]initWithString:[formatter stringFromDate:AlarmDateUno]];
NSMutableString *alarmString = [[NSMutableString alloc]initWithString:@""];
NSString *temp = [[NSString alloc] initWithString:[formatter stringFromDate:AlarmDateUno]];
[alarmString appendString:temp];
[LabelalarmUno setText:alarmString];


    if(clicked == 0) 
        {            
            clicked =1;
            [btOnOffUno  setImage:[UIImage imageNamed:@"OnBtn.png"] forState:UIControlStateNormal];
                bellOne.hidden = NO;

             }

    else 
        { 
            clicked = 0;
            [btOnOffUno  setImage:[UIImage imageNamed:@"OffBtn.png"] forState:UIControlStateNormal];
                bellOne.hidden = YES; 
                isAlarmUnoOn = 0;

对于设置闹钟后的合并时间,请使用:

   // CONTROLLO TEMPO UNO

   - (void)runTimerUno {

            myTickerUno = [NSTimer scheduledTimerWithTimeInterval: 0.5
                                            target: self
                                          selector: @selector(showActivityUno)
                                          userInfo: nil
                                           repeats: YES];

     }

  - (void)showActivityUno {
  NSDateFormatter *formatter =
        [[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];

if(isAlarmUnoOn == 1 && AlarmUnoPlaying ==0){
    NSLog(@"Going to compare times Uno");
    NSDate *d = [date earlierDate:AlarmDateUno];
    NSLog(@"Compared Times Uno");


    if (d == date) {
        NSLog(@"Current time earlier Uno");
    } 
    else if (d == AlarmDateUno) {
        AlarmUnoPlaying = 1;

        [self playAlarmSoundUno];
    }               

}
[formatter setTimeStyle:NSDateFormatterMediumStyle];  

}

我尝试将您的代码集成到我的代码中但没有成功。

1 个答案:

答案 0 :(得分:1)

这会将选择器的时间添加到当前日期,然后如果时间过去则添加一天,以便警报始终在将来:

AlarmDateUno                = [alarmDatePickerUno date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; 

// Get the current date & time, and modify it to the beginning of the day (midnight).
NSDate *currentDate = [NSDate date];
NSDateComponents *currentDateComponents = [currentCalendar components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
currentDate = [currentCalendar dateFromComponents:currentDateComponents];

// Add the time  from the picker to the currentDate (midnight value) to give us today's date with the picker time.
AlarmDateUno = [currentDate dateByAddingTimeInterval:[AlarmDateUno timeIntervalSinceReferenceDate]];

// Now, if AlarmDateUno is before the current time, assume that we mean tomorrow and add one day:
if ([currentDate compare:AlarmDateUno] == NSOrderedAscending) {
    // Add one day to the current alarm time.
    NSDateComponents *oneDay = [[NSDateComponents alloc] init];
    [oneDay setDay:1];
    AlarmDateUno = [currentCalendar dateByAddingComponents:oneDay toDate:AlarmDateUno options:0];
}