ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController < //普通协议代理,处理视图选取普通事件协议功能 UIPickerViewDelegate, //数据代理协议 获取数据代理函数 UIPickerViewDataSource > @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //创建选择视图对象 //显示多数组和多个元素以供选择 //日期 时间 日历 //地区地址功能 UIPickerView* pickview=[[UIPickerView alloc]init]; pickview.frame=CGRectMake(10, 100, 300, 200); //设置普通代理为当前视图控制器 pickview.delegate=self; //数据代理对象为当前视图控制器 pickview.dataSource=self; [self.view addSubview:pickview]; } // 返回3组 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } // 每组10行 //P1 调用此协议的选择视图本身 //P2 缔结组元素个数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; { return 10; } //显示元素内容 -(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSString* str = [NSString stringWithFormat:@"%dzu %dhang",component+1,row+1]; return str; } //设置每行高度 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 100; } @end