UIDatePicker日期选择问题

时间:2011-08-17 07:30:21

标签: iphone objective-c

如何在日期选择器中显示开始日期和结束日期,并选择它们以在iPhone中执行某些事件

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date  
NSDate *pickerDate = [self.datePicker date];

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:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];

1 个答案:

答案 0 :(得分:1)

您可以创建一个包含两行的UITableView,一行用于开始,一行用于结束日期。然后在didSelectRowAtIndexPath中将datePicker.tag设置为indexPath.row。在启动UIControlEventValueChanged时,您可以简单地从UIDatePicker获取NSDate并将其分配给您的变量。