2012-08-14 63 views
0

我試圖實現一個從UIDatePicker(僅限時間,無日期)設置UILocalNotification的警報類型的應用程序。我有一種工作方式,除了當用戶嘗試設置鬧鐘時假設它是5:00 PM。如果他們嘗試設置鬧鐘爲4:45 PM (讓我們假設他們在第二天這樣做),它不會設置UILocalNotification。我確信這是因爲我的代碼,特別是「setHour」或「setMinute」,或者我有我的nsdates日曆的方式。如果有人能告訴我我需要做些什麼才能使它工作,這將不勝感激。UILocalNotification從picker中觸發

中的UIDatePicker的名字是

datePick 

謝謝!

UILocalNotification *futureAlert; 
futureAlert = [[UILocalNotification alloc] init]; 
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 
NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
//[dateComps setDay:item.day]; 
//[dateComps setMonth:item.month]; 
//[dateComps setYear:item.year]; 
[dateComps setHour:11]; 
[dateComps setMinute:46]; 
NSDate *itemDate = [calendar dateFromComponents:dateComps]; 
futureAlert.fireDate = [datePick date];  
futureAlert.timeZone = [NSTimeZone defaultTimeZone]; 
futureAlert.alertBody= @"time to wake up"; 
[[UIApplication sharedApplication] scheduleLocalNotification:futureAlert]; 
[futureAlert release]; 

回答

1

嘗試使用此行代碼:

double interval = [datePick.date timeIntervalSinceNow]; // time interval 
if (interval < 0) // if time is earling at this time 
    interval += 86400; // for set this time at next day 
futureAlert.fireDate = [[NSDate date] dateByAddingTimeInterval: interval]; 
futureAlert.timeZone = [NSTimeZone systemTimeZone]; 

其中間隔你的情況將等於85500秒。 24小時內86400秒,15分鐘-900秒,然後是23小時45分鐘--85500秒(86400-900)。

所以,你可以計算間隔秒數,並使用它

+0

感謝您的答覆。對不起,你有點失去了我與「間隔」。你有什麼建議我宣佈,作爲/再做一次?謝謝 – 2012-08-14 23:24:36

+0

代碼已更新 – CReaTuS 2012-08-14 23:26:45

+0

謝謝。只是爲了驗證你的設置,因爲在我的例子中,我說的是下午4:45,這是23小時45分鐘的差異。但請記住它是一個鬧鐘應用程序。我無法控制用戶選擇設置鬧鐘的時間。差異不會總是23小時45分鐘。還有其他建議嗎?謝謝 – 2012-08-14 23:34:34