UIPickerView是類似於選擇日期的滾動輪
代碼如下
先簽訂UIPickerView的兩個協議,delegate和dataSource,並且定義兩個數組
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 30, 375, 289)];
pickerView.backgroundColor = [UIColor redColor];
[self.view addSubview:pickerView];
[pickerView release];
pickerView.delegate = self;
pickerView.dataSource = self;
self.arr1 = @[@"zhangsna",@"lisi",@"sdf"];
self.arr2 = @[@"123",@"125",@"7497"];
顯示的列數
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
判斷行數
- (NSinteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component == 0){
return self.arr1.count;
}else{
return self,arr2.count;
}
}
顯示內容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component == 0){
return self.arr1[row];
}else{
return self.arr2.[row];
}
}
點擊方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"%@",self.arr2[row]);
}