2012-03-24 69 views
0

對不起谷歌翻譯的出錯,很多錯誤,測試報警正確的時間

我想使在Xcode報警,但我AVE問題,萬物正常工作,如果我使用一個選擇器視圖的日期和時間,但如果我切換選擇器視圖在一個只有時間,這是不可能的設置鬧鐘,因爲如果我設置鬧鐘在一次之前,現在立即響起,

我怎麼能解決不使用選擇器視圖與日期?

這是設置鬧鐘的動作。

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

}

我嘗試將你的代碼整合到我的程序中,但沒有成功

+0

請使用正確的英語。我不明白你的意思。 – dasdom 2012-03-24 21:18:13

+0

不好意思谷歌翻譯犯錯誤,很多錯誤, 我想在Xcode中發出警報,但我有一個問題,萬物工作正常,如果我用日期和時間選擇器視圖,但如果我切換選擇器視圖在一個只有時間,這是不可能的設置鬧鐘,因爲如果我立即設置鬧鐘,現在立即響起, 我怎麼能解決沒有使用選擇器視圖與日期? 這是設置鬧鐘的動作。 PS對不起,預覽消息也許我的英語更好,然後谷歌翻譯... – Acunamatata 2012-03-24 21:29:29

回答

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]; 
} 
+0

是正是我的意思......但我不明白我可以使用這部分代碼?什麼是「CurrentDate」。感謝您的幫助! – Acunamatata 2012-03-24 21:53:18

+0

我修改了原始答案中的代碼。它應該是你正在尋找的東西! – lnafziger 2012-03-24 22:02:57

+0

問題是我用它來執行鬧鐘 – Acunamatata 2012-03-24 23:00:53