正如 rmaddy 所说,使用 UIPickerView。并使用像这样的适当委托方法自定义选择器的数据源。
设置日期来源(例如)
soureYear = [[NSMutableArray alloc] initWithObjects:@"2001",@"2002",@"2003",@"2004",@"2005",@"2006",@"2007",@"2008",@"2009",@"2010",@"2011",@"2012",@"2013", nil];
soureMonth = [[NSMutableArray alloc] initWithObjects:@"Jan",@"Feb",@"Mar",@"Apl",@"May",@"Jun",@"July",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec", nil];
委托方法。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 1)
return [soureMonth count];
else
return [soureYear count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
return [soureYear objectAtIndex:row];
return [soureMonth objectAtIndex:row];
}
并像这样从选择器中获取选定的项目
-(IBAction)selectedItem
{
NSLog(@" %@", [soureYear objectAtIndex:[sample2 selectedRowInComponent:0]]);
NSLog(@" %@", [soureMonth objectAtIndex:[sample2 selectedRowInComponent:1]]);
}
你会得到你所期望的。