2010-12-10 63 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 = ...進行設置,以便您可以正確訪問它們。