gpt4 book ai didi

iphone - UIDatePicker 仅显示年份

转载 作者:技术小花猫 更新时间:2023-10-29 10:15:22 31 4
gpt4 key购买 nike

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

最佳答案

我有一些我正要删除的代码,但如果它在某个地方在线,则该片段会更好。这并不惊人,但可以搜索到!

用于创建自 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]];
}

UIPickerView 数据源和委托(delegate)方法:

- (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];
}

不要忘记接口(interface)中的声明

//Data
NSMutableArray *years;

它的输出是

enter image description here

引用自here

关于iphone - UIDatePicker 仅显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017791/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com