2011-04-25 63 views
2

我在UItableview中有一個文本框。用戶點擊文本字段,我想要一個UIpickerview從NSArray中彈出數據。我試圖實現這個,但應用程序崩潰。 請在下面找到我的代碼。對textField的UIPickerview點擊

- (NSString*)pickerView:(UIPickerView*)pv titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [policyarraycount objectAtIndex:row]; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pv 
{ 
    return 1; 
} 
- (NSInteger)pickerView:(UIPickerView*)pv numberOfRowsInComponent:(NSInteger)component 
{ 
    return [policyarraycount count]; 
} 

- (void)pickerView:(UIPickerView *)pv didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 

    txtPolicy.text = (NSString *)[policyarraycount objectAtIndex:row]; 
} 

請讓我知道我要去哪裏錯了。

感謝

回答

1

我就在幾個月前用來爲應用程序:

建立一個IBAction爲,然後創建UIPickerview有:

UIPickerView *thingPicker = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
[UIPickerView setAnimationDelegate:self]; 
thingPicker.delegate = self; 
thingPicker.dataSource = self; 
[thingPicker setShowsSelectionIndicator:YES]; 
textField.inputView = thingPicker; 

,並設置選擇器的參數:

(帶有textFieldNumber可以將每個pickerView設置爲不同的textField)

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)windPicker { 

return 1; //Here if textFieldBOOL == # can also be used but I only use 1 component. 
} 

- (NSInteger)pickerView:(UIPickerView *)thingPicker numberOfRowsInComponent:(NSInteger)component { 
if (textFieldNumber == 0) { 
    return [fromAnArray count]; 

- (NSString *)pickerView:(UIPickerView *)thingPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
if (textFieldNumber == 0){ 
    return [fromAnArray objectAtIndex:row]; 
} 

- (void)pickerView:(UIPickerView *)thingPicker didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
if (textFieldNumber == 0) { 
[textField setText:(NSString *)[fromAnArray objectAtIndex:row]];