如何为UIPickerView设置数据?

时间:2013-01-26 22:31:27

标签: iphone xcode uipickerview

我正在尝试添加PickerView - 但我看到一个黑屏......我做错了什么???

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];

    [self.view addSubview:myPickerView];

1 个答案:

答案 0 :(得分:5)

实施UIPickerViewDataSource方法:

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

UIPickerViewDelegate方法:

– pickerView:titleForRow:forComponent:

不要忘记设置属性:

UIPickerView * myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
[self.view addSubview:myPickerView];

在.h文件中,记得放:

<UIPickerViewDelegate, UIPickerViewDataSource>

我通常使用填充字符串的NSMutableArray来填写数据。希望有所帮助!