我无法使用UIDatePicker。我只是想在所有评论者告诉我只使用UIDatePicker之前将其抛弃。我希望有一个Picker Wheel,显示一年中的所有日期,直到今天,并显示为Day:1 / Jan 1,Day:2 / Jan 2,依此类推。以下是我到目前为止的内容,它可以检测当年的“天数”,并在PickerWheel中填入今天所有日期:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSString *pickerItemTitle = [NSString stringWithFormat: @"Day: %d", index];
[options addObject: pickerItemTitle];
}
我可以做些什么来使NSInteger与日期匹配,并且它也是pickerItemTitle的一部分?
答案 0 :(得分:0)
弄清楚了。添加了一些代码,以形成基于日期整数的日期到循环中。现在,为uipickerview设置数组的viewWillAppear方法如下所示:
- (void)viewWillAppear:(BOOL)animated
{
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:index];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MMM dd"];
NSString *articleDateString = [formatter stringFromDate:date];
NSString *pickerItemTitle = [NSString stringWithFormat: @"Day: %d/%@", index, articleDateString];
[options addObject: pickerItemTitle];
}
[super viewWillAppear:YES];
}