我已为用户选择了UIPickerView
来选择他们的感受;生气,快乐,悲伤等。但我无法将他们选择的内容转换为字符串或文本。是否有可能做到这一点?如果不能如何保存他们在选择器中选择的行/选项?
答案 0 :(得分:0)
如果我理解你的问题,这里需要的是什么。通过此gitHub commit或download zip
查看项目typedef enum
:创建一个包含所有情绪值的枚举;所有人都喜欢分配给每个人的独特价值:标题文件
//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;
标题文件
//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;
}
}
标题文件
//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])];
}