当用户同时旋转组件时,UIPickerView会崩溃应用程序
UIPickerView让我遇到了无法忍受的问题。 有两个组成部分:第一个是食品类别,第二个是每个类别中的食品。 我有适当的食物arrays,看起来像:
ViewController.h
@property (strong, nonatomic) NSArray *leftPickerDataSource; @property (strong, nonatomic) NSArray *vegetablesDataSource; @property (strong, nonatomic) NSArray *eggsDataSource; @property (strong, nonatomic) NSArray *pastaDataSource; @property (strong, nonatomic) NSArray *riceDataSource; @property (strong, nonatomic) NSArray *meatDataSource;
ViewController.m
... @implementation ViewController @synthesize foodPicker; @synthesize leftPickerDataSource; @synthesize vegetablesDataSource; @synthesize eggsDataSource; @synthesize pastaDataSource; @synthesize riceDataSource; @synthesize meatDataSource; - (void)viewDidLoad { [super viewDidLoad]; self.leftPickerDataSource = [NSArray arrayWithObjects: @"Vegetables", @"Eggs", @"Pasta", @"Rice", @"Meat", nil]; self.vegetablesDataSource = [NSArray arrayWithObjects:@"Potatoes", @"Broad bean", @"Beans", @"Broccoli", @"Cabbage", @"Cauliflower", @"Corn", nil]; self.eggsDataSource = [NSArray arrayWithObjects:@"Soft-boiled", @"Hard-boiled", nil]; self.pastaDataSource = [NSArray arrayWithObjects:@"Pasta", @"Spaghetti", nil]; self.riceDataSource = [NSArray arrayWithObjects:@"White", @"Brown", @"Black", @"Red", nil]; self.meatDataSource = [NSArray arrayWithObjects:@"Sausages", @"Crabs", @"Lobsters", @"Shrimps", nil]; }
我相信数组不是问题,但是当我同时旋转选择器的两个组件时,应用程序通常会与EXC_BAD_ACCESS(代码= 1 …)崩溃。
这是我的UIPickerView:
ViewController.m
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { // Left picker return [leftPickerDataSource count]; //[foodPicker selectRow:0 inComponent:1 animated:YES]; } else { // Right picker NSInteger sRow = [foodPicker selectedRowInComponent:0]; if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Vegetables"]) return [vegetablesDataSource count]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Eggs"]) return [eggsDataSource count]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Pasta"]) return [pastaDataSource count]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Rice"]) return [riceDataSource count]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Meat"]) return [meatDataSource count]; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { // Left picker return [leftPickerDataSource objectAtIndex:row]; } else { // Right picker NSInteger sRow = [foodPicker selectedRowInComponent:0]; if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Vegetables"]) return [vegetablesDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Eggs"]) return [eggsDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Pasta"]) return [pastaDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Rice"]) return [riceDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:sRow] isEqual:@"Meat"]) return [meatDataSource objectAtIndex:row]; } } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) [foodPicker reloadComponent:1]; }
在这里找不到任何可疑的东西。 不知道为什么,但旋转车轮会使应用程序崩溃。 我在SO寻找答案,但没有任何帮助:/
你有什么想法吗?
实际上我的假设是错的 – 选中的行不是未定义的或者是nil,但是当你旋转时它会快速变化。 问题是你在numberOfRowsInComponent中返回的数字取决于该方法运行时旋转拨号的位置,但是当titleForRow运行时,第一个组件位于不同的行上,并且您正在尝试访问不在存在。 要修复它,你应该定义一个属性sRow,在numberOfRowsInComponent中为它赋值,然后在titleForRow中使用相同的值(不要像你现在那样重新分配),而不是获取一个新值。
@property (nonatomic) NSInteger sRow; - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { // Left picker return [leftPickerDataSource count]; //[foodPicker selectRow:0 inComponent:1 animated:YES]; } else { // Right picker self.sRow = [foodPicker selectedRowInComponent:0]; if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Vegetables"]) return [vegetablesDataSource count]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Eggs"]) return [eggsDataSource count]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Pasta"]) return [pastaDataSource count]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Rice"]) return [riceDataSource count]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Meat"]) return [meatDataSource count]; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { // Left picker return [leftPickerDataSource objectAtIndex:row]; } else { // Right picker if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Vegetables"]) return [vegetablesDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Eggs"]) return [eggsDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Pasta"]) return [pastaDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Rice"]) return [riceDataSource objectAtIndex:row]; else if ([[leftPickerDataSource objectAtIndex:self.sRow] isEqual:@"Meat"]) return [meatDataSource objectAtIndex:row]; } }
如果你想两者一起滚动,制作两个组件的目的是什么?
如果您的第一个组件值取决于第0个组件,那么理想情况下将多个组件滚动到一起应该没有任何意义。
您可以使多个选择器具有单个组件。
更新 :还有一个选项,您可以实现逻辑,直到用户选择第0个组件应用程序不会显示第一个组件,称为依赖uipickerview。 看看这个问题: 问题