2013-11-15 29 views
0

我正在處理loacl通知通知,並且成功完成。但我不想設置從選擇器通知我想設置通知firetime哪個用戶想要通知。我給了文本字段輸入時間,並希望按用戶時間設置。設置用戶輸入本地通知的時間

假設用戶進入下午1點00分那麼今天通知正值下午1點

下面

是我的代碼 -

NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar 
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date 
[components setHour:txtStartTime.text]; 
// [components setMinute:12]; 

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [calendar dateFromComponents:components]; 
localNotification.timeZone = [NSTimeZone localTimeZone]; 
localNotification.alertBody = txtSubjectName.text; 
localNotification.alertAction = @"Lecture Notification"; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 

回答

0

您可以通過使用NSDateFormatter字符串轉換爲一個NSDate然後使用NSDate的作爲fireDate。但是,如果用戶完全按照預期輸入日期,它將會工作得很好。這裏是它如何工作的代碼示例:

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; 
NSDate *myDate = [df dateFromString: myDateAsAStringValue]; 

這裏正顯示出可用,因此您可以創建自己的輸入風格不同的日期格式模式網站:http://waracle.net/iphone-nsdateformatter-date-formatting-table/

您應該使用一個UIDatePicker有用戶可以輕鬆輸入日期和時間。這裏是一個教程,解釋如何使用它:http://www.edumobile.org/iphone/iphone-programming-tutorials/add-datepicker-programmatically-and-display-date-in-iphone/