Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 420
user1523344 Avatar asked Sep 06 '12 07:09

user1523344


People also ask

What is the purpose of the delegate of a Uipicker view object?

Typically the delegate implements other optional methods to respond to new selections or deselections of component rows. See UIPickerView for a discussion of components, rows, row content, and row selection.

What is UIPickerView?

What is UIPickerView ? UIPickerView is a subclass of UIView . It is useful for iOS apps that use a spinning wheel with many choices for the user. @MainActor class UIPickerView: UIView. One example of this type of app is a slot machine, where a user spins the wheels to play the game.


2 Answers

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
{
}
like image 93
Venk Avatar answered Sep 28 '22 16:09

Venk


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>

like image 38
tiguero Avatar answered Sep 28 '22 17:09

tiguero