.h
#import@interface YYWViewController : UIViewController @property (retain, nonatomic) IBOutlet UIPickerView *pickerView;@property(retain,nonatomic)IBOutlet UILabel *selectLabel;@property (strong, nonatomic) NSArray *pickerData;@end
.m
#import "YYWViewController.h"@interface YYWViewController ()@end@implementation YYWViewController@synthesize pickerView;@synthesize selectLabel;-(void) addPickerView { NSArray *array = [[NSArray alloc] initWithObjects:@"",@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil]; _pickerData = array; if (pickerView == nil) { pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 240, 320, 460)]; pickerView.delegate = self; pickerView.dataSource =self; pickerView.showsSelectionIndicator = YES; [self.view addSubview:pickerView]; [pickerView release]; } } -(void) addLabel { if (selectLabel == nil) { selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 40)]; selectLabel.text = @""; selectLabel.font = [UIFont systemFontOfSize:12]; selectLabel.textAlignment = UITextAlignmentCenter; selectLabel.backgroundColor = [UIColor grayColor]; [self.view addSubview:selectLabel]; [selectLabel release]; } }// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad { [super viewDidLoad]; [self addPickerView]; [self addLabel]; }#pragma mark pickerView delegate//返回pickerview的组件数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView { return 1;}//返回每个组件上的行数- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component { return [_pickerData count];}//设置每行显示的内容- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [_pickerData objectAtIndex:row]; }//当你选中pickerview的某行时会调用该函数。- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { NSLog(@"You select row %d",row); selectLabel.text = [NSString stringWithFormat:@"you select %@",[_pickerData objectAtIndex:row]];}//设置指定组件上每行的宽度/* -(void)pickerView:(UIPickerView *)thePickerView widthForComponet:component { } */- (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}- (void)dealloc { [pickerView release]; [super dealloc];}@end