UIPickerView繼承自UIView站绪,所以不能像UIControl樣綁定事件處理方法,所以UIPickerView的事件處理由其委托對(duì)象完成丽柿。
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
_myPicker = [[UIPickerView alloc] init];
//設(shè)置高亮顯示
_myPicker.showsSelectionIndicator = YES;
//數(shù)據(jù)源
_myPicker.dataSource = self;
//代理
_myPicker.delegate = self;
//放在正中間
_myPicker.center = self.view.center;
//顯示的時(shí)候是第幾個(gè),索引是從0開(kāi)始的恢准,顯示第5個(gè)
[_myPicker selectRow:4 inComponent:0 animated:YES];
//第二列魂挂,也是第5個(gè)
[_myPicker selectRow:4 inComponent:1 animated:YES];
[self.view addSubview:_myPicker];
numberOfComponents:獲取指定列中的列表項(xiàng)的數(shù)量,只讀屬性
showsSelectionIndicato:是否顯示UIPickerView中的選中標(biāo)記(以高亮背景作為選中標(biāo)記)
-numberOfRowsInComponent: 獲取列的數(shù)量
-rowSizeForComponent: 獲取指定列中列表項(xiàng)的大小馁筐。返回CGSize對(duì)象
-selectRow:inComponent:animated:設(shè)置指定列的特定列表項(xiàng)涂召。是否使用動(dòng)畫(huà)
UIPickerViewDataSource :
控制包含多少列,每一列又包含多少列表項(xiàng)(也是是行)
//設(shè)置數(shù)據(jù)源方法敏沉,返回2表示有兩列
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//設(shè)置每一列果正,有多少行
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//先定義一個(gè)變量
NSInteger result;
//判斷
if (component == 0) {
//是第一列的時(shí)候,給5行
result = 5;
} else if (component == 1){
//第二列盟迟,給10行
result = 10;
}
//返回?cái)?shù)值
return result;
}
//設(shè)置協(xié)議方法
//每一列寬度
// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 150;
}
//每一行顯示的數(shù)據(jù)
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//返回的是NSString類型的數(shù)據(jù)
//返回每一行舱卡,因?yàn)樗饕菑?開(kāi)始,+1
NSString *result = [NSString stringWithFormat:@"我是第%ld行",(long)row + 1];
return result;
}
UIpickerView.gif
PS:話說(shuō)這個(gè)大概可以和日期相結(jié)合吧队萤,回頭試一下