标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (void)layoutUI { 21 UIPickerView *pikVCustom = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; 22 pikVCustom.center = self.view.center; 23 pikVCustom.dataSource = self; 24 pikVCustom.delegate = self; 25 [pikVCustom selectRow:4 inComponent:0 animated:YES]; //设置第一个组件列选中第五行 26 [self.view addSubview:pikVCustom]; 27 } 28 29 #pragma mark - PickerView 30 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 31 return 3; //返回组件列数 32 } 33 34 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 35 return 10; //返回组件行数 36 } 37 38 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 39 return [NSString stringWithFormat:@"%ld-%ld", (long)component, (long)row]; 40 } 41 42 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 43 NSInteger selectedRowInFirstComponent = [pickerView selectedRowInComponent:0]; 44 NSInteger selectedRowInSecondComponent = [pickerView selectedRowInComponent:1]; 45 NSInteger selectedRowInThirdComponent = [pickerView selectedRowInComponent:2]; 46 47 NSString *strMessage = [NSString stringWithFormat:@"0-%ld, 1-%ld, 2-%ld", 48 selectedRowInFirstComponent, 49 selectedRowInSecondComponent, 50 selectedRowInThirdComponent]; 51 UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"选择的内容" 52 message:strMessage 53 delegate:nil 54 cancelButtonTitle:nil 55 otherButtonTitles:@"确定", nil]; 56 [alertV show]; 57 } 58 59 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4579284.html