ios - UIPicker查看 NSDictionary 中的数据

标签 ios objective-c xcode nsdictionary uipickerview

我有这样的网络服务日期:

{
            "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 做到这一点?

编辑:

我的要求是:

  1. 从 JSON 中获取“idlife_stage_question”和“question_text”。
  2. 在 NSMutableDictionary 中设置“idlife_stage_question”和“question_text”
  3. 使用“question_text”填充 UIPicker
  4. 在标签中显示选定行的文本
  5. 从 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/

相关文章:

ios - 从应用程序调用后顶部的额外空间

objective-c - 为什么将 useSingleLineMode 设置为 YES 的 NSTextField 具有多行文本的固有内容大小?

ios - UIView 与 UIViewController 的绘图

ios - UIWindow 中存在 UIAlertContoller ios8

objective-c - c/objective-c 中的数组指针

c# - 如何访问 UITableViewSource 下的 NavigationController.PushViewController?

ios - 如何在 swift 4 中按下按钮时将 json 结果发送到电子邮件?

ios - 在 iOS 7 上使用覆盖的 setEditing 方法时抑制 UITableViewCell 删除按钮

ios - 无法将类型 'CustomProfileCell' 的返回表达式转换为返回类型 'UITableViewCell'

iphone - 使用正则表达式在 Xcode 中查找/替换