我想做一个简单的闹钟。
在此代码部分中,从pickerdate(唯一的时间选择器)中获取日期。
如果闹钟是我添加一天之前的当前日期。
// ALARM UNO
// Set Allarme Uno
-(void)viewDidAppear:(BOOL)animated{
NSDate *pickerDate = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
//Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
[alarmDatePickerUno setDate:itemDate animated:YES];
}
-(IBAction)setAlarmUno:(id)sender{
NSLog(@"picker in allarme");
NSDateFormatter *formatter =
[[[NSDateFormatter alloc] init] autorelease];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
AlarmDateUno = [alarmDatePickerUno date];
NSLog(@"allarme settata alle");
NSLog(@"%@", AlarmDateUno);
if ([alarmDatePickerUno.date compare:[NSDate date]] == NSOrderedAscending)
{
NSLog(@"Date is not valid");
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
NSLog(@"Aggiungo un giorno");
AlarmDateUno = [currentCalendar dateByAddingComponents:oneDay toDate:AlarmDateUno options:0];
NSLog(@"%@", AlarmDateUno);
//return;
}
之后我将当前日期与闹钟进行比较......
// CONTROLLO TEMPO UNO
- (void)runTimerUno {
myTickerUno = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(showActivityUno)
userInfo: nil
repeats: YES];
}
- (void)showActivityUno {
// NSDate *date = [NSDate date];
if(isAlarmUnoOn == 1 && AlarmUnoPlaying ==0){
NSLog(@"Confronto data e tempo");
if ([[[NSDate date] earlierDate:AlarmDateUno] isEqualToDate: AlarmDateUno])
{
AlarmUnoPlaying = 1;
[self playAlarmSoundUno];
}
}
}
这是选择器视图中的日期中的问题是在当前时间之前,我添加了一个小时我在该行收到EXC_BAD_ACCESS错误:
if ([[[NSDate date] earlierDate:AlarmDateUno] isEqualToDate: AlarmDateUno])
僵尸说我 [__NSDate timeIntervalSinceReferenceDate]:发送到解除分配的实例0x914b710的消息
我尝试每一件事,我不明白为什么......任何人都可以帮助我吗?
答案 0 :(得分:1)
看来AlarmDateUno是一个属性。那是对的吗?如果是这样,您需要在设置时保留它 - 否则,在您尝试再次访问它之前可能会将其取消分配。