我有这样的网络服务日期:
{
"idlife_stage_question" = 43;//..basically this should be key
"life_stage_idlife_stage" = 1;
"profile_idprofile" = 0;
"question_text" = "example";//..this should be object
sequence = 42;
}
现在我需要每个 JSON 字典中的“idlife_stage_question”和“question_text”,并在 UIPickerView 中加载“question_text”,然后在 UILabel 中显示所选行文本。此外,我需要获取相应的“question_text”的“idlife_stage_question”,以便稍后可以将“idlife_stage_question”发送到服务器。我如何使用 NSDictionary 做到这一点?
编辑:
我的要求是:
- 从 JSON 中获取“idlife_stage_question”和“question_text”。
- 在 NSMutableDictionary 中设置“idlife_stage_question”和“question_text”
- 使用“question_text”填充 UIPicker
- 在标签中显示选定行的文本
- 从 NSMutableDictionary 中获取“question_text”对应的“idlife_stage_question”,并将其发送到服务器。
最佳答案
假设您有 self.questionsArray
,其中包含来自 Web 服务的所有数据,并且 UIPickerView
中只有一个组件
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.questionsArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.questionsArray[row][@"question_text"];
}
关闭pickerView的方法
- (void)dismissPickerView
{
//Get the selected Row in picker
NSInteger selectedQuestionIndex = [self.pickerView selectedRowInComponent:0];
NSDictionary *question = self.questionsArray[selectedQuestionIndex];
self.label.text = question[@"question_text"];
//Use this value to send back to server
NSInteger questionId = [question[@"idlife_stage_question"] integerValue];
}
关于ios - UIPicker查看 NSDictionary 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16186009/