2011-03-21 5 views
5

Aquí estoy añadiendo un pickerview programaticlycómo administrar este selector ver una fuente de datos

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     CGRect pickerFrame = CGRectMake(0,280,321,200); 

     UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure 

     //Set up the picker view as you need it 

     //Set up the display frame 
     myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions 

     //Add the picker to the view 
     [self.view addSubview:myPickerView]; 
    } 

Pero ahora tengo que realmente haga que sea mostrar contenidos y de alguna manera saber cuando se cambia y qué valor se ha cambiado a. ¿Cómo hago esto?

+0

echar un vistazo en los documentos de xcode para UIPickerView datasource y delegados. – MCannon

Respuesta

18

en lugar de archivos .h este código

@interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

asignar el origen de datos y delegar en el selector de

// this view controller is the data source and delegate 
myPickerView.delegate = self; 
myPickerView.dataSource = self; 

utilizan los siguientes métodos delegado y datasouce

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 

} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 
{ 
    return 200; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 
{ 
    return 50; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSString *returnStr = @""; 
    if (pickerView == myPickerView) 
    {  
     returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:@"nodeContent"]; 
    } 

    return returnStr; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (pickerView == myPickerView) 
    { 
     return [levelPickerViewArray count]; 
    } 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 
0

Debe crear una clase que implemente el protocolo UIPickerViewDataSource y asignarle una instancia a myPickerView.dataSource.

+0

¿Cómo asigno la instancia? – Michael

+0

@Michael Acabas de establecer myPickerView.dataSource = (instancia de tu fuente de datos). De dónde proviene la instancia de fuente de datos es específica de su aplicación. Solo recuerde que UIPickerView no conserva su dataSource, por lo que deberá crearlo/conservarlo en su controlador de visualización (si no en otro lugar). – Tony

Cuestiones relacionadas