如何在 iOS 应用程序中使用 MMPickerView 按年份、型号、制作方式获取 Vehicle API 的层次结构
Posted
技术标签:
【中文标题】如何在 iOS 应用程序中使用 MMPickerView 按年份、型号、制作方式获取 Vehicle API 的层次结构【英文标题】:How to get the hierarchy of Vehicle API by year, model, make using MMPickerView for an iOS app 【发布时间】:2015-02-10 14:10:58 【问题描述】:我使用了 ccpickerview,但现在决定更改为 MMPickerView。但我无法根据所选年份传递车辆品牌和型号的 json 数据。我可以从 jsonData 得到年份。当我点击 Make of the Vehicles 的选择器时,应用程序因此错误而崩溃;
加载
2015-02-10 21:51:22.105 VehicleApp[2099:872363] PostData: year=2013&tag=vehMake
2015-02-10 21:51:22.728 VehicleApp[2099:872363] Response code: 200
2015-02-10 21:51:22.731 Vehicle[2099:872363] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[6]'
*** First throw call stack:
网络服务很好,因为当我使用 CCPickerView 时,条件非常简单“从 json 中拉出的车辆。这就是我想要的
在我之前的 CCPickerView 课程中,我遇到了这种情况;
- (void)pickerView:(CPPickerView *)pickerView didSelectItem:(NSInteger)item
if (self.yearsPicker == pickerView)
[self setVahicleModel:[self.years objectAtIndex:item]];
Yyear =[self.years objectAtIndex:item];
if (self.modelPicker == pickerView)
Mmake =[self.model objectAtIndex:item];
[self setVahicle:Mmake Varient:Yyear];
if (self.varientPicker == pickerView)
Mmodel =[self.varient objectAtIndex:item];
if (self.insurerPicker == pickerView)
Iinsurer =[self.insurer objectAtIndex:item];
if (self.financePicker == pickerView)
Ffinance =[self.finance objectAtIndex:item];
如何使用 MMPickerView 获得相同的结果? 这是我的 ViewDidLoad
- (void)viewDidLoad
[super viewDidLoad];
[self setVehicleYear];
// [self setVehicleMake];
// [self setVehicleModel];
_selectedYear = [years objectAtIndex:0];
_selectedMake = [make objectAtIndex:0];
_selectedModel = [model objectAtIndex:0];
任何想法或建议我在下面的代码中做错了什么;
- (IBAction)selectYear:(id)sender
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:years
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:_selectedYear
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblYear.text = [selectedObject description];
_selectedYear = selectedObject;
];
- (IBAction)selectMake:(id)sender
[self setVehicleMake];
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:make
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:_selectedMake
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblMake.text = [selectedObject description];
_selectedMake = selectedObject;
];
- (IBAction)selectModel:(id)sender
[self setVehicleModel];
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:model
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:_selectedModel
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblModel.text = [selectedObject description];
_selectedModel = selectedObject;
];
【问题讨论】:
【参考方案1】:这就是我所做的,使用 MMPicketView 并且工作正常,希望这个解决方案对其他人有所帮助。
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setVehicleYear];
selectedYear = [years objectAtIndex:0];
_selectedInsurer = [insurer objectAtIndex:0];
_selectedFinance = [finance objectAtIndex:0];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)selectYear:(id)sender
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:years
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:selectedYear
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblYear.text = [selectedObject description];
selectedYear = selectedObject;
[self setVehicleMake:selectedYear];
selectedMake = [make objectAtIndex:0];
];
- (IBAction)selectMake:(id)sender
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:make
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:selectedMake
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblMake.text = [selectedObject description];
selectedMake = selectedObject;
[self setVehicleModel:selectedMake Varient:selectedYear];
selectedModel = [varient objectAtIndex:0];
];
- (IBAction)selectModel:(id)sender
//PickerView using array of objects, with selectedObject functionality
[MMPickerView showPickerViewInView:self.view
withObjects:varient
withOptions:@MMbackgroundColor: [UIColor blackColor],
MMtextColor: [UIColor whiteColor],
MMtoolbarColor: [UIColor blackColor],
MMbuttonColor: [UIColor whiteColor],
MMfont: [UIFont systemFontOfSize:18],
MMvalueY: @3,
MMselectedObject:selectedModel
objectToStringConverter:^NSString *(id object)
return [object description];
completion:^(id selectedObject)
_lblModel.text = [selectedObject description];
selectedModel = selectedObject;
];
【讨论】:
您可以通过单击绿色对勾来接受您的答案。这标志着问题在 Stack Overflow 中已解决。以上是关于如何在 iOS 应用程序中使用 MMPickerView 按年份、型号、制作方式获取 Vehicle API 的层次结构的主要内容,如果未能解决你的问题,请参考以下文章