我决定使用UIPickerView来模拟一种下拉菜单。 我的问题是,我想使用多个UIPickerView。 我如何设置第二个UIPickerView? 添加一个新的UIViewController?或者它是如何工作的? 添加第二个跨度是不够的,我需要多达3个UIPickerViews。 在此先感谢:)
代码:
-(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"];
}
答案 0 :(得分:2)
代码更新:
// 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
}