`我正在创建一个UIPickerView,它不会从数组中获取数据,因为没有调用委托方法。我设置了UIPickerviewdelegate和datasource但是它仍然没有工作。可能是没有调用委托方法的原因?????如果有人知道它请告诉我。谢谢你。
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 :(得分: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
{
}
答案 1 :(得分:4)
通常当有一个未调用的委托方法时,有三个原因:
您没有将类设置为委托。在为选择器对象分配内存之前,你似乎已经但是设置了你的委托。听起来很神奇。
您没有在符合委托协议的类中实现委托方法。
您没有将您的类设置为符合委托协议(意味着您没有写这样的内容
@interface yourClass : whateverurclassheritfrom <UIPickerViewDelegate>
答案 2 :(得分: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];
问题是,您在delegate
datasource
之前设置了allocating
和picker
。