如何将UIPickerview中的选定行更改为字符串(文本)?

时间:2015-08-02 18:15:26

标签: ios uipickerview

我已为用户选择了UIPickerView来选择他们的感受;生气,快乐,悲伤等。但我无法将他们选择的内容转换为字符串或文本。是否有可能做到这一点?如果不能如何保存他们在选择器中选择的行/选项?

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,这里需要的是什么。通过此gitHub commitdownload zip

查看项目
  1. 使用typedef enum创建一个包含所有情绪值的枚举;所有人都喜欢分配给每个人的独特价值:
  2. 。快乐,好,中立,悲伤,不快乐等等

    标题文件

    //Number Values for each value of emotion. Add new emotions to the bottom of list, sort will not
    //be effected in this declartion
    typedef enum : NSUInteger {
        CTEmotionNone = 0,
        CTEmotionHappy = 1,
        CTEmotionOkay = 2,
        CTEmotionNeutral = 3,
        CTEmotionSad = 4,
        CTEmotionUnhappy = 5,
        CTEmotionVeryHappy = 6
    
    } CDEmotions;
    
    1. 将数字值翻译为文字:有不同的方法来做这部分,但我喜欢这个。创建一个内联方法或函数,它将为相应的情感值返回一个字符串:
    2. 标题文件

      //Here is where the title for each number value will be transalated
      static inline NSString * NSEmotionTitleForEmotion( CDEmotions emotion) {
          switch (emotion) {
              case CTEmotionNone:
                  return @"Empty"; break;
              case CTEmotionVeryHappy:
                  return @"Very Happy"; break;
              case CTEmotionHappy:
                  return @"Happy"; break;
              case CTEmotionOkay:
                  return @"Okay"; break;
              case CTEmotionNeutral:
                  return @"Neutral"; break;
              case CTEmotionSad:
                  return @"Sad"; break;
              case CTEmotionUnhappy:
                  return @"Unhappy"; break;
      
          }
      
      }
      
      1. 现在订单:创建另一个内联方法,但这次返回的数组中包含您要显示情感的顺序:
      2. 标题文件

        //Sorted int values in this array. Sort will be shown here. Do not put text in here, because then
        //to know what "Very Happy" is you'll ahve to compare it to a string vs just saying 1 == 1, that
        //means CTEmotionHappy
        static inline NSArray * NSEmotionsList() {
            return [NSArray arrayWithObjects:
                    @(CTEmotionNone),
                    @(CTEmotionVeryHappy),
                    @(CTEmotionHappy),
                    @(CTEmotionOkay),
                    @(CTEmotionNeutral),
                    @(CTEmotionSad),
                    @(CTEmotionUnhappy), nil];
        
        }
        

        现在使用这些函数会很有趣:)我会在你的情况下使用UIPickerViewDataSource中的- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component执行此操作:

        ViewController.h

        - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
            return 1;
        
        }
        
        - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
            return [NSEmotionsList() count];
        
        }
        
        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
            return NSEmotionTitleForEmotion( [[NSEmotionsList() objectAtIndex: row] intValue]);
        
        }
        

        最后实现委托方法- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
            [labelEmotion setText: NSEmotionTitleForEmotion( [[NSEmotionsList() objectAtIndex: row] intValue])];
        
        }