2012-09-06 13 views
6

`Sto creando un UIPickerView che sta arrivando ma non sta prendendo i dati dalla matrice come i metodi delegati non vengono chiamati. Ho impostato UIPickerviewdelegate e datasource ma ancora non funziona. Qual è la ragione per cui non si chiamano i metodi delegati? Se qualcuno ne è a conoscenza, per favore, dimmelo. Grazie in anticipo.perché i metodi delegati UIPickerView non vengono chiamati anche dopo aver impostato i protocolli delegati?

picker.delegate=self; 
picker.dataSource=self; 



arrayOfState = [[NSMutableArray alloc] init]; 
[self.arrayOfState addObject:@"Assam"]; 
[arrayOfState addObject:@"Andhra Pradesh"]; 
[arrayOfState addObject:@"Madhya Pradesh"]; 
[arrayOfState addObject:@"West Bengal"]; 
[self.arrayOfState addObject:@"Orissa"];  
[self.arrayOfState addObject:@"Meghalaya"]; 
[arrayOfState addObject:@"Tripura"]; 
[arrayOfState addObject:@"Arunachal Pradesh"]; 
[arrayOfState addObject:@"Jammu and Kashmir"]; 

picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,100.0f,40.0f,120.0f)]; 
picker.showsSelectionIndicator=YES; 
+0

inviare le parti specifiche del codice in questione. Altrimenti nessuno sarà in grado di offrire molto aiuto. –

+0

dopo che ho implementato i metodi delegate come..numberOfComponentsInPickerView, didselectrow, numberofRowsinComponent ecc .... ma questi non vengono chiamati nella mia applicazione ... – user1523344

+0

@ user1523344, Come hai aggiunto UIPickerView? Aggiunto in IB o creato programmaticamente? – Hemang

risposta

6

provare questo,

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
arrayOfState = [[NSMutableArray alloc] init]; 
[self.arrayOfState addObject:@"Assam"]; 
[arrayOfState addObject:@"Andhra Pradesh"]; 
[arrayOfState addObject:@"Madhya Pradesh"]; 
[arrayOfState addObject:@"West Bengal"]; 
[self.arrayOfState addObject:@"Orissa"];  
[self.arrayOfState addObject:@"Meghalaya"]; 
[arrayOfState addObject:@"Tripura"]; 
[arrayOfState addObject:@"Arunachal Pradesh"]; 
[arrayOfState addObject:@"Jammu and Kashmir"]; 

picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,100.0f,40.0f,120.0f)]; 
picker.delegate=self; 
picker.dataSource=self; 
picker.showsSelectionIndicator=YES; 
[self.view addSubView:picker]; 
} 

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

// Total rows in our component. 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return [arrayOfState count]; 
} 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 

     NSString *title; 
     title=[arrayOfState objectAtIndex:row]; 
     return title; 
    } 


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

Di solito quando c'è un metodo delegato che non si chiama ci sono tre ragioni per questo:

  1. Lei non ha fissato una classe di essere il delegato. Sembra che tu abbia fatto MA impostando il tuo delegato prima di allocare memoria per il picker l'oggetto mi sembra magico.

  2. Non sono stati implementati i metodi delegati nella classe che sarà conforme al protocollo delegato.

  3. Non hai impostato la tua classe conforme al protocollo delegato (il che significa non scrivere qualcosa di simile

@interface yourClass : whateverurclassheritfrom <UIPickerViewDelegate>

1

Add objects to array before calling below code

picker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0f,100.0f,40.0f,120.0f)]; 
picker.delegate=self; 
picker.dataSource=self; 
picker.showsSelectionIndicator=YES; 
[self.view addSubView:picker]; 

Il problema è stato così, hai impostato delegate e datasource prima del allocatingpicker.

Problemi correlati