Using more than one UIPickerView
I decided to use a UIPickerView to simulate a kind of dropdown menu. My problem is, that i want to use more than one UIPickerView. How do i manage to set up a second UIPickerView? Adding a new UIViewController? or how does it work? Adding a second span won't be enough, i need up to 3 more UIPickerViews. Thanks in advance :)
Code:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerarray count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerarray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
}
- (void)viewDidLoad {
pickerarray = [[NSMutableArray alloc] init];
[pickerarray addObject:@"Hamburg"];
[pickerarray addObject:@"Berlin"];
[pickerarray addObject:@"München"];
[pickerarray addObject:@"Bonn"];
pickerarray2 = [[NSMutableArray alloc] init];
[pickerarray2 addObject:@"Hambburg"];
开发者_开发问答[pickerarray2 addObject:@"Berelin"];
[pickerarray2 addObject:@"Müneechen"];
[pickerarray2 addObject:@"Boneen"];
}
- Add all of them into your view.
- Set them as hidden.
- Set unique tags for each of them.
- Add buttons that "reveal them".
- Implement delegate methods that manage values depending on picker view tag.
Code update:
// put this before view controllers @implementation
enum {
MyFirstPickerViewTag = 1,
MySecondPickerViewTag
} MyPickerViewTags; // fix for warning
// assign tags 1 and 2 respectively to your picker views in IB
// here's one of delegate methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
switch(pickerView.tag) {
case MyFirstPickerViewTag: {
return 2;
}
case MySecondPickerViewTag: {
return 3;
}
}
return 1; // component by default
}
精彩评论