在中介绍了UIPickerView的用法,当时选择的两组数据是没有任何关联的,可以任意组合,下面介绍限制的方法,就是第一个选择器的每个选项,都有不同的子选项。代码大部分与前文相似,只是实现了UIPickerViewDelegate协议中的-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component方法,这个方法会在选择操作后触发,当用户在第一个选择器选择时,就会触发这个方法,我们在这个方法中重新载入第二个选择器的数据,即可。下面的代码与原教程有些不同,但思路是一样的。 viewController.h:
#import
@interface BIDDependentComponentPickerViewController : UIViewController
@property (strong,nonatomic) NSArray * type1;
//存储第一个选择器的数据
@property (strong,nonatomic)NSArray * subtype;
//存储第二个选择器的数据,这个随着第一个选择器被选中的值而改变
@property (strong,nonatomic) NSArray * subtype1;
@property(strong,nonatomic)NSArray * subtype2;
@property (strong,nonatomic)NSArray * subtype3;
//上面三个用于存储第二个选择器的数据,根据第一个选择器选定的值,调用其中一个赋值给subtype
@property (strong, nonatomic) IBOutlet UIPickerView *dependentPicker;
- (IBAction)buttonPressed;
@end
viewController.m:
#import "BIDDependentComponentPickerViewController.h"
@implementation BIDDependentComponentPickerViewController
@synthesize dependentPicker;
@synthesize type1=_type1;
@synthesize subtype1=_subtype1;
@synthesize subtype2=_subtype2;
@synthesize subtype3=_subtype3;
@synthesize subtype=_subtype;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array=[NSArray arrayWithObjects:@"a",@"b",@"c", nil];
_type1=array;
NSArray *subarr1=[NSArray arrayWithObjects:@"aa",@"aaa",@"aaaa", nil];
_subtype1=subarr1;
NSArray *subarr2=[NSArray arrayWithObjects:@"bb",@"bbb",@"bbbb", nil];
_subtype2=subarr2;
NSArray *subarr3=[NSArray arrayWithObjects:@"cc",@"ccc",@"cccc", nil];
_subtype3=subarr3;
_subtype=subarr1;
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setDependentPicker:nil];
_subtype1=nil;
_subtype2=nil;
_subtype3=nil;
_type1=nil;
_subtype=nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed {
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return [_type1 count];
}
return [_subtype count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component==0)
return [_type1 objectAtIndex:row];
return [_subtype objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component==0)
{
if (row==0) {
_subtype=_subtype1;
}else if(row==1)
{
_subtype=_subtype2;
}else{
_subtype=_subtype3;
}
[dependentPicker selectRow:0 inComponent:1 animated:YES];
//使第二个选择器选中第一项,并且开启动画效果
//如果没有上面这句,即便第一个选择器选项已经改变,第二个选择器的selectedRow还是不会变
[dependentPicker reloadComponent:1];
//重新载入第二个选择器
}
}
@end