ios UIDatePicker 只显示年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12017791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:46:29  来源:igfitidea点击:

UIDatePicker to show year only

iphoneobjective-cios

提问by Jay

Is there a way to have a UIDatePicker show year only?

有没有办法让 UIDatePicker 只显示年份?

回答by The iOSDev

I have a little code that I was about to delete, but the snippet is better off if it is online somewhere. It's not amazing, but it is searchable!

我有一小段代码要删除,但如果该代码段在某个地方在线,则效果会更好。这并不神奇,但它是可搜索的!

Objective-C code to create an array of all years since 1960. Perfect for input into a UIPicker

用于创建自 1960 年以来所有年份的数组的 Objective-C 代码。非常适合输入到UIPicker

//Get Current Year into i2
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:@"yyyy"];
int i2  = [[formatter stringFromDate:[NSDate date]] intValue];


//Create Years Array from 1960 to This year
years = [[NSMutableArray alloc] init];
for (int i=1960; i<=i2; i++) {
    [years addObject:[NSString stringWithFormat:@"%d",i]];
}

The UIPickerViewdata source and delegate methods:

UIPickerView数据源和委托方法:

- (NSInteger)numberOfComponentsInPickerView: (UIPickerView*)thePickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    return [years count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView
titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [years objectAtIndex:row];
}

Don't forget the declaration in the interface

不要忘记接口中的声明

//Data
NSMutableArray *years;

The out put of it is

它的输出是

enter image description here

在此处输入图片说明

Referenced from here

这里引用

回答by James Zaghini

You can't do this with UIDatePicker, but you can with UIPicker.

你不能用UIDatePicker,但你可以用UIPicker

You need to create an array for the years, and add them to the UIPickerusing the UIPickerdelegates

您需要为这些年创建一个数组,并将它们添加到UIPicker使用UIPicker委托中

Here's a tutorial.

是一个教程。

回答by cowbear16

I cannot comment, but I was playing around with the answer by Wolvorin and I decided to do a way where the most recent year would be at the top of the Picker. Currently, his code goes oldest to most recent years.

我无法发表评论,但我正在考虑 Wolvorin 的答案,我决定采取一种方式,使最近一年处于 Picker 的首位。目前,他的代码最古老到最近几年。

All I changes was in my NSMutableArray setup in the viewDidLoad instead of his:

我所做的所有更改都在 viewDidLoad 中的 NSMutableArray 设置中,而不是他的:

//Create Years Array from 1960 to This year
years = [[NSMutableArray alloc] init];
for (int i=1960; i<=i2; i++) {
    [years addObject:[NSString stringWithFormat:@"%d",i]];
}

I used:

我用了:

years = [[NSMutableArray alloc] init];
for (int i1=i2; i1<=i2 & i1>=1920; i1--) {
    [years addObject:[NSString stringWithFormat:@"%d",i1]];
}

Which does the for in reversed order going from the most recent date, then subtracting one year (and adding it to the Array) until it gets to 1920.

从最近的日期开始以相反的顺序执行 for,然后减去一年(并将其添加到数组中)直到它到达 1920。

My formula:

我的公式:

int i1=i2; i1<=i2 & i1>=1920; i1--

回答by Sharukh Mastan

Just updating the existing answer in this post with Swift 3:

只需使用Swift 3更新这篇文章中的现有答案:

var yearsTillNow : [String] {
    var years = [String]()
    for i in (1970..<2018).reversed() {
        years.append("\(i)")
    }
    return years
}

Just use this for your UIPickerViewdatasource.

只需将此用于您的UIPickerView数据源。

回答by Robin

I changed Sharukh Mastan's code slightly to always show the current year on top.

我稍微更改了 Sharukh Mastan 的代码,以始终在顶部显示当前年份。

var formattedDate: String? = ""

let format = DateFormatter()
format.dateFormat = "yyyy"
formattedDate = format.string(from: date)

var yearsTillNow: [String] {
    var years = [String]()
    for i in (Int(formattedDate!)!-70..<Int(formattedDate!)!+1).reversed() {
        years.append("\(i)")
    }
    return years
}

print(yearsTillNow)

This prints an array of years going back for 70 years which you can use as UIPickerView datasource

这将打印可以追溯到 70 年的年份数组,您可以将其用作 UIPickerView 数据源