本文介绍了UIDatePicker仅显示年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让UIDatePicker只显示年份?
Is there a way to have a UIDatePicker show year only?
推荐答案
我有一些我要做的代码删除,但如果它在某个地方在线,则片段会更好。这并不奇怪,但它是可以搜索的!
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!
自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
数据源和委托方法:
- (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];
}
不要忘记界面中的声明
//Data
NSMutableArray *years;
它的输出是
从
这篇关于UIDatePicker仅显示年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!