2010-12-10 62 views
1

我有一个uipickerview用于在uitextfield中显示名称,以及关于如何将隐藏于用户的值传递给变量的任何想法。我可以以某种方式将值与NSArray中的名称关联,但不显示它。 谢谢UIPickerView数据

尝试一个字典,而是我坚持的东西,可能是非常简单的,我有一本字典

NSArray *keys = [NSArray arrayWithObjects:@"1", @"2", nil]; 
NSArray *objects = [NSArray arrayWithObjects:@"3", @"4", nil]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

和didSelectRow我有

NSString *itemKey = [NSString stringWithFormat:@"%f", row]; 
    survonemd.text = [dictionary objectForKey:itemKey]; 

这似乎确定

但我卡在titleForRow

任何帮助或提示将不胜感激。

我现在有 -

@property (nonatomic, retain) NSArray *keys; 
@property (nonatomic, retain) NSArray *objects; 


@synthesize keys; 
@synthesize objects; 


self.keys = [NSArray arrayWithObjects:@"1", @"2", @"3", nil]; 
self.objects = [NSArray arrayWithObjects:@"4", @"5", @"6", nil]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
NSString *itemKey = [keys objectAtIndex:row]; 
return [dictionary objectForKey:itemKey]; 
} 

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
NSString *itemKey = [keys objectAtIndex:row]; 
survonemd.text = [dictionary objectForKey:itemKey]; 
} 

它运行和选择器视图会从文本字段名为但没有数据显示在选择器。请帮忙。

+0

提供一些代码可能会有帮助,但我会建议使用NSDictionary,因为它允许您有键和值。您可以将键显示在UIPickerView上,每个键对应于用户隐藏的值。 – Jumhyn 2010-12-10 08:10:44

+0

感谢Jumhyn,我正在尝试使用字典,但我坚持的东西应该很简单。我有一本字典 – Mattog1456 2010-12-14 14:58:14

回答

1

代替生成从该行号码键串(您会使用%i代替%f的方式)的,使用行作为索引键数组:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    NSString *itemKey = [keys objectAtIndex:row]; 
    survonemd.text = [dictionary objectForKey:itemKey]; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *itemKey = [keys objectAtIndex:row]; 
    return [dictionary objectForKey:itemKey]; 
} 

这样,关键值也不一定是数字作为字符串。

将您的密钥和字典对象定义为属性,并使用self.keys = ...进行设置,以便您可以正确访问它们。