简体   繁体   中英

why UIPickerView delegate methods are not called even after setting delegate protocols?

`I am creating a UIPickerView which is comming but its not taking the data from array as the delegate methods are not called. I set UIPickerviewdelegate and datasource but still it not working.What may be the reason for not calling the delegate methods?????If somebody knows about it please 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;

try this,

- (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
{
}

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

  1. You didn't set a class to be the delegate. It seems you did BUT setting your delegate before allocating memory for the picker object sounds magical to me.

  2. You didn't implement the delegate methods in the class that will conform to the delegate protocol.

  3. You didn't set your class as conforming to the delegate protocol (meaning you didn't write something like this

@interface yourClass : whateverurclassheritfrom <UIPickerViewDelegate>

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 setting delegate and datasource before allocating picker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM