1.UIPickerView的宽度和高度是固定的,纵向是320*216,横向是568*162
2.属性:
@property(nonatomic,readonly)NSIntegernumberOfComponents; // 选择框的行数
@property(nonatomic,assign)id<UIPickerViewDataSource> dataSource; (类似于UITableView)
@property(nonatomic,assign)id<UIPickerViewDelegate> delegate; (类似于UITableView)
(BOOL)showsSelectionIndicator // 是否显示选择指示器 ,即是一个蓝色的条
- pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
- // 指定Delegate
- pickerView.delegate=self;
- // 显示选中框
- pickerView.showsSelectionIndicator=YES;
- [self.view addSubview:pickerView];
选取器上显示数据,必须依赖两个协议,UIPickerViewDelegate和UIPickerViewDataSource,把他们添加到ViewController.h文件中
- #import <UIKit/UIKit.h>
- @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
- {
- UIPickerView *pickerView;
- NSArray *pickerData;
- }
- @end
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
- // 指定Delegate
- pickerView.delegate=self;
- // 显示选中框
- pickerView.showsSelectionIndicator=YES;
- [self.view addSubview:pickerView];
- NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil];
- pickerData=dataArray;
- // 添加按钮
- CGRect frame = CGRectMake(120, 250, 80, 40);
- UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- selectButton.frame=frame;
- [selectButton setTitle:@"SELECT" forState:UIControlStateNormal];
- [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:selectButton];
- }
4.实现UIPickerView的代理方法,将数据显示在选取器上所需要几个方法
- #pragma mark -
- #pragma mark Picker Date Source Methods
- //返回显示的列数
- -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- {
- return 1;
- }
- //返回当前列显示的行数
- -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- {
- return [pickerData count];
- }
- #pragma mark Picker Delegate Methods
- //返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
- -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- {
- return [pickerData objectAtIndex:row];
- }
说一下两个协议实例方法,参考http://www.cnblogs.com/edsioon/
UIPickerViewDelegate中的实例方法
// 当用户选择某个row时
- (void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
// 当其在绘制row内容,需要row的高度时
- (CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
// 返回指定component.row显示的文本
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
// 当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view
- (UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
// row的宽度
- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
按照官方文档的说法,UIPickerViewDataSource这个协议仅有的功能就是提供picker view中component的个数和各个component中的row的个数,虽然名为datasource,但是它工作于MVC的C中
本协议仅有两个实例方法,均需要实现:
// 返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
// 返回每一列对应的行数
- (NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
5.关于按钮响应事件,关于按钮的形成和添加响应事件不再提,前面都有,
- -(void) buttonPressed:(id)sender
- {
- NSInteger row =[pickerView selectedRowInComponent:0];
- NSString *selected = [pickerData objectAtIndex:row];
- NSString *message = [[NSString alloc] initWithFormat:@"你选择的是:%@",selected];
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
- message:message
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles: nil];
- [alert show];
- }
@UIPickerView还有其他实例方法
// 获取指定列的行数
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
// 刷新所有的列
- (void) reloadAllComponents
// 刷新指定的列
- (void) reloadComponent: (NSInteger) component
- (CGSize) rowSizeForComponent: (NSInteger) component
// 获取某列选择的行数
- (NSInteger) selectedRowInComponent: (NSInteger) component
// 选择一行
- (void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
- (UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
附上源代码: http://download.csdn.net/detail/duxinfeng2010/4410909