2013-01-05 88 views
0

我無法使用UIDatePicker。在所有評論者告訴我只使用UIDatePicker之前,我只想在開始時拋出它。我想要一個Picker Wheel來顯示今年的所有今年的日子,並將其顯示爲日期:1/1月1日,日:2/1月2日等等。這裏是我到目前爲止,檢測的一年它是什麼日子「數字」,並填充撿拾器與所有的日期領先到今天:UIPickerView自定義日期

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

想通了。添加了一些代碼,以基於整天的整數形成日期到循環中。現在,設置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]; 
}