6

`我正在创建一个正在运行的 UIPickerView,但它没有从数组中获取数据,因为没有调用委托方法。我设置了 UIPickerviewdelegate 和数据源,但仍然无法正常工作。不调用委托方法的原因可能是什么??????如果有人知道,请告诉我。提前谢谢。

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

3 回答 3

6

试试这个,

- (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
{
}
于 2012-09-06T08:30:06.563 回答
4

通常当有一个未调用的委托方法时,有以下三个原因:

  1. 您没有将班级设置为代表。似乎您在为选取器对象分配内存之前设置了您的委托,这对我来说听起来很神奇。

  2. 您没有在符合委托协议的类中实现委托方法。

  3. 你没有将你的类设置为符合委托协议(这意味着你没有写这样的东西

@interface yourClass : whateverurclassheritfrom <UIPickerViewDelegate>

于 2012-09-06T08:00:16.457 回答
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];

问题是,你设置delegatedatasource之前allocating picker

于 2012-09-06T08:03:09.563 回答