UIPickerView 遇到了非常难以忍受的问题。有 2 个组件:第一个包含食物类别,第二个包含每个类别中的食物。我有适当的食物阵列,如下所示:
视图控制器.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;
视图控制器.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 (Code=1...) 而崩溃。
这是我的 UIPickerView:
视图控制器.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 上寻找答案,但没有任何帮助:/
你知道怎么回事吗?