UIPickerView没有连接到DataSource

时间:2011-06-20 18:54:53

标签: iphone objective-c cocoa-touch uipickerview

所以我的UIPickerView没有连接到DataSource。我不知道为什么。

我使用UIPickerViewDelegateUIPickerViewDataSource创建了一个文件,并正确执行了在点击textField时引入UIPickerView的过程。

pickerView有效,但不显示任何组件。我已经实施了

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

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 



- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 


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


- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

知道为什么pickerView没有连接到DataSource吗?我也在所有这些方法中尝试过NSLogging。 // Does not print

我也设置了

packSizePopPickerView.delegate = self;
packSizePopPickerView.dataSource = self;
packSizePopPickerView = [[UIPickerView alloc] init];

2 个答案:

答案 0 :(得分:1)

在cellForRowAtIndexPath中,您正在设置packSizePopPickerView的dataSource / delegate,然后将packSizePopPickerView分配/初始化为新的UIPickerView。新的UIPickerView不再设置其委托/ dataSource。

这是错误的:

packSizePopPickerView.delegate = self;
packSizePopPickerView.dataSource = self;
packSizePopPickerView = [[UIPickerView alloc] init];

试试这个:

packSizePopPickerView = [[UIPickerView alloc] init];
packSizePopPickerView.delegate = self;
packSizePopPickerView.dataSource = self;

答案 1 :(得分:0)

您需要明确告诉选择器其数据源和委托是什么。

myPicker.delegate = someController; myPicker.datasource = someController;