Why aren't UIPickerView delegate methods called even after delegated protocols are installed?

ʻI create a UIPickerView that is sent, but does not accept data from the array, as delegate methods are not called. I install UIPickerviewdelegate and datasource, but still not working. What could be the reason for not calling delegate methods ????? If anyone knows about this, tell me. Thanks in advance.

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; 
+6
source share
3 answers

try it,

 - (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 { } 
+6
source

Usually, when there is a delegate method that is not called, there are three reasons for this:

  • You did not define the class as a delegate. It seems you did BUT configure your delegate before allocating memory for the picker to me.

  • You have not implemented delegate methods in the class that will conform to the delegate protocol.

  • You did not set your class according to the delegate protocol (this means you are not writing something like this

@interface yourClass : whateverurclassheritfrom <UIPickerViewDelegate>

+3
source

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

The problem was that you set the delegate and datasource to the allocating picker .

+1
source

Source: https://habr.com/ru/post/924706/


All Articles