【发布时间】:2013-11-20 07:11:32
【问题描述】:
我正在为 iOS 中的月份和年份准备 UIPickerView。
下面是我的代码。
在 viewdidload 中:
//Array for picker view
monthsArray=[[NSMutableArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *yearString = [formatter stringFromDate:[NSDate date]];
yearsArray=[[NSMutableArray alloc]init];
for (int i=0; i<13; i++)
{
[yearsArray addObject:[NSString stringWithFormat:@"%d",[yearString intValue]+i]];
}
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[myPickerView selectRow:0 inComponent:0 animated:YES];
[self.view addSubview:myPickerView];
选取器视图委托方法:
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger rowsInComponent;
if (component==0)
{
rowsInComponent=[monthsArray count];
}
else
{
rowsInComponent=[yearsArray count];
}
return rowsInComponent;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString * nameInRow;
if (component==0)
{
nameInRow=[monthsArray objectAtIndex:row];
}
else if (component==1)
{
nameInRow=[yearsArray objectAtIndex:row];
}
return nameInRow;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
CGFloat componentWidth ;
if (component==0)
{
componentWidth = 100;
}
else {
componentWidth = 100;
}
return componentWidth;
}
我得到以下输出:
但是在今年,从一月到十月的月份已经到期。如何在我的选择器中动态禁用当前年份的那些年份。这些月份应该可以用于剩余的年份。
其实真正的输出是,
在上面,应在 UI 中禁用当年过期的月份。
我们将不胜感激。
提前致谢。
【问题讨论】:
标签: ios objective-c uipickerview uidatepicker