2011-07-09 24 views
41

¿Cómo configuro programáticamente UIPickerView en una vista sin usar el Generador de interfaz? También tiene problemas para entender cómo trabajar con las porciones delegadas de UIPickerView.Ejemplo programático UIPickerView?

+0

Aquí es un ejemplo bien documentado y completo que funciona en iOS 10 https://stackoverflow.com/a/46047257/3634990 – jungledev

Respuesta

26

Para añadir UIPickerView programación:

- (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; 
} 
+0

¿Cómo agrego 3 listas de diferentes objetos a 3 componentes en el selector? ¿Cómo recupero cada artículo seleccionado por componente? gracias –

+0

He editado mi respuesta. Puedes consultar ahora. – AppAspect

+0

Te di un punto porque agrega más información al enlace de otros muchachos ... gracias –

15

Esto no funcionará en iOS 8

Crear UIPickerView programación con DataSource

archivo .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 

archivo .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

+2

¿cómo hacer que funcione en iOS8? – user1872384

2
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];