UIPickerView
(1)
先声明要用的协议<UIPickerViewDataSource,UIPickerViewDelegate>
声明变量
@property (strong,nonatomic) NSArray * books;
@property (strong,nonatomic) NSArray * authors;
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
- (void)viewDidLoad
{
[super viewDidLoad];
_authors = [NSArray arrayWithObjects:@"li", @"zhang", @"liu",nil];
_books = [NSArray arrayWithObjects:@"111", @"222", @"333",nil];
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;//两列,有几列返回几
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return _authors.count;
}
return _books.count;
}//返回每一列的数目
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return [_authors objectAtIndex:row];
}
return [_books objectAtIndex:row];
}//返回选择的列的选择的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSArray * tmp = component == 0 ? _authors : _books;
NSString * tip = component == 0 ? @"authers " : @"books";
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"notice" message:[NSString stringWithFormat:@"you have chose %@ : %@",tip,[tmp objectAtIndex:row]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}//获取选择的内容
(2)相互依赖的Picker
@property (strong,nonatomic) NSDictionary * books;
@property (strong,nonatomic) NSArray * authors;
@property (strong,nonatomic) NSString * selectedAuthor;
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
- (void)viewDidLoad
{
[super viewDidLoad];
_books = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"111", @"222",nil], @"li", [NSArray arrayWithObjects:@"333", @"444",nil], @"zhang", [NSArray arrayWithObjects:@"555", @"666", @"777",nil], @"liu",nil];
_authors = [[_books allKeys]sortedArrayUsingSelector:@selector(compare:)];
_selectedAuthor = [_authors objectAtIndex:0];
self.picker.dataSource = self;
self.picker.delegate = self;
}
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return _authors.count;
}
return [[_books objectForKey:_selectedAuthor]count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return [_authors objectAtIndex:row];
}
return [[_books objectForKey:_selectedAuthor] objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == 0) {
_selectedAuthor = [_authors objectAtIndex:row];
[self.picker reloadComponent:1];
}
NSArray * tmp = component == 0 ? _authors : [_books objectForKey:_selectedAuthor];
NSString * tip = component == 0 ? @"auther" : @"books";
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"notice" message:[NSString stringWithFormat:@"you have chose %@ : %@",tip,[tmp objectAtIndex:row]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}