UIPickerView编程示例?

如何在不使用Interface Builder的情况下以编程方式在视图中设置UIPickerView? 同时也很难理解如何使用UIPickerView的委托部分。

以编程方式添加UIPickerView:

- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if(component == 1) selectedRowPicker1 = row; else if(component == 2) selectedRowPicker2 = row; else selectedRowPicker3 = row; } - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 3; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(component == 1) return [list1 count]; else if(component == 2) return [list2 count]; else return [list3 count]; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if(component == 1) val1 = [list1 objectAtIndex:row]; else if(component == 2) val2 = [list2 objectAtIndex:row]; else val3 = [list3 objectAtIndex:row]; return strTemp; } 

这在iOS 8中不起作用

使用DataSource以编程方式创buildUIPickerView

.h文件

 @interface PickerViewTestAppViewController : UIViewController<UIPickerViewDelegate, UIPickerViewDataSource> { UIActionSheet *pickerViewPopup; UIPickerView *categoryPickerView; UIPickerView *pickerView; NSMutableArray *dataArray; } @property (nonatomic, retain) UIActionSheet *pickerViewPopup; @property (nonatomic, retain) UIPickerView *categoryPickerView; @property (nonatomic, retain) NSMutableArray *dataArray; @end 

.m文件

 @implementation PickerViewTestAppViewController @synthesize pickerViewPopup,categoryPickerView; @synthesize dataArray; - (void)viewDidLoad { [super viewDidLoad]; // Init the data array. dataArray = [[NSMutableArray alloc] init]; // Add some data for demo purposes. [dataArray addObject:@"One"]; [dataArray addObject:@"Two"]; [dataArray addObject:@"Three"]; [dataArray addObject:@"Four"]; [dataArray addObject:@"Five"]; pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; categoryPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; [categoryPickerView setDataSource: self]; [categoryPickerView setDelegate: self]; categoryPickerView.showsSelectionIndicator = YES; UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; pickerToolbar.barStyle = UIBarStyleBlackOpaque; [pickerToolbar sizeToFit]; UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(categoryDoneButtonPressed)]; UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(categoryCancelButtonPressed)]; [pickerToolbar setItems:@[cancelBtn, flexSpace, doneBtn] animated:YES]; [pickerViewPopup addSubview:pickerToolbar]; [pickerViewPopup addSubview:categoryPickerView]; [pickerViewPopup showInView:self.view]; [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)]; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component { // Handle the selection NSLog(@"%@",[dataArray objectAtIndex:row]); selectedCategory = [NSString stringWithFormat:@"%@",[dataArray objectAtIndex:row]]; } // tell the picker how many rows are available for a given component - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [dataArray count]; } // tell the picker how many components it will have - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } // tell the picker the title for a given component - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [dataArray objectAtIndex: row]; } // tell the picker the width of each row for a given component - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { int sectionWidth = 300; return sectionWidth; } -(void)categoryDoneButtonPressed{ categoryLable.text = selectedCategory; [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; } -(void)categoryCancelButtonPressed{ [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; } 

Ref: http : //gabriel-tips.blogspot.in/2011/04/uipickerview-add-it-programmatically_04.html

  NSInteger selectCourse=[_coursePicker selectedRowInComponent:0]; NSInteger selectSem=[_coursePicker selectedRowInComponent:1]; NSString *whatCourse=[Course objectAtIndex:selectCourse]; NSString *whatSem=[Sem objectAtIndex:selectSem]; NSString *courses=[[NSString alloc]initWithFormat:@"Course : %@ - %@",whatCourse,whatSem]; [self.selectedCours setTitle:courses forState:UIControlStateNormal];