3

很抱歉,因为这已经问过了,但我就是无法理解它。

我有一个工作正常的 UIPickerView,我将它添加到我的 UIViewController

    _pv = [[CategoryPicker alloc] init];
    _textField.inputView = _pv;

_pv 是 UIPickerView 和 _textField 是我希望结果出现的文本字段。

所以我当我使用这行代码时:

_pv.categoryPicker.delegate = self; //set in my viewDidLoad

然后将其放置在 UIViewController 中:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection

_textField.text = [_pv.categoryArray objectAtIndex:row];

}   

我能够填充 TextField,但 UIPickerView 填充了“?” 对于数组的每个条目。

现在当我这样做时:

_pv.categoryPicker.delegate = _pv.self; //set in my viewDidLoad

我把它放在 UIPickerView 中:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection

_outputArray = [_categoryArray objectAtIndex:row];
NSLog(@"The item selected = %@", _outputArray);


 }

UIPickerView 已填充,但数据未传递到 TextField。

所以这就是我卡住的地方——我需要在两个地方都有结果。现在我想我用 subClassing 来做到这一点,但是我读过的所有内容都对我不起作用。

请帮我:-)

提前谢谢。

4

3 回答 3

2

您应该查看UIPickerViewDelegate协议的完整文档。

基本上,您要做的是将 UIViewController 设置为 UIPickerView 的委托,如下所示:

_pv.categoryPicker.delegate = self;

然后在您的 UIViewController 中,您要实现以下 UIPickerViewDelegate 方法:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // called when a row is selected
    _textField.text = [_pv.categoryArray objectAtIndex:row];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    // here is where you populate the contents of the pickerView
    // return the title that you want to appear in 'row'
     return [NSString stringWithFormat:@"row %d", row];
}
于 2012-06-28T23:12:06.620 回答
1

我最近实现了类似的东西。I have a UIPickerView that loads values from an array and when a row inside the UIPicker is selected, it updates the value inside a cell in a UITableView. 但是您也可以轻松地更新文本字段中的值:

头文件内部:

在你的头文件中,添加:

// You will not need to set the view as the delegate again.

@interface yourClass: UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIPickerView *_pv;
    UITextField *_textField;
}

@property (nonatomic, retain) IBOutlet UIPickerView *_pv;

在您的实现中:

@synthesize _pv;

    #pragma mark -
    #pragma mark View Lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Initialize the array
        _categoryArray = [[NSArray alloc] initWithObjects:
                        @"1", @"2", @"3",
                        @"4", @"5", nil];

        // Initialize text field of type UITextField and set its input type as picker viewer
        _textField.inputView = pickerViewer; 
     }

#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   // The array with all the values that you want to see inside the picker viewer
   return [_categoryArray count]; 
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   // Value inside the picker viewer will be loaded for the output array
   return [_categoryArray objectAtIndex:row];
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // When a value is selected in picker viewer, take its value and assign it to the text view
    //NSLog(@"Picker view row selected");
    _textField.text = [NSString stringWithFormat:@"%@", [_categoryArray objectAtIndex:row]]; 
}

还要在 IB 中的视图上拖动一个选择器查看器,并将其连接到文件所有者内的 _pv,因为它是一个 IBOutlet。

希望能帮助到你。我在 UITablewViewController 中实现了我的,并且在同一个视图中有多个 UIPickerViewers。所以我编辑了一些代码,如果我错过了什么并且它不起作用,请告诉我。

于 2012-06-29T02:24:15.560 回答
0

我不知道我是否正确理解了您的问题,但是为了填充UIPickerView您需要执行以下操作:

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{  
    return [_categoryArray objectAtIndex:row];   
}
于 2012-06-28T21:42:16.753 回答