我在我的应用程序中实现了一个简单的UIPickerView,并且我不断收到警告,我无法解释有关不完整的实现和“协议中的方法未完成”。我经历了一系列的例子,无法弄清楚我错过了什么。
这是我的代码:
·H
@interface ViewTestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource> {
UIPickerView *pickerView;
NSMutableArray *pickerList;
....
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *pickerList;
@end
的.m
- (void)viewDidLoad
{
[super viewDidLoad];
…
//PICKER AREA
pickerList = [[NSMutableArray alloc] init];
[pickerList addObject:@"aaa"];
[pickerList addObject:@"bbb"];
CGRect pickerFrame = CGRectMake(0, 200, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
}
-(IBAction)showPicker:(id)sender {
[self.view addSubview:pickerView];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerList count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerList objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Selected item: %@ index of selected item: %i", [pickerList objectAtIndex:row], row);
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 200;
}
答案 0 :(得分:4)
你需要符合.h中的UIPickerViewDelegate
和UIPickerViewDataSource
,就像你为UITableViewDelegate,UITableViewDataSource等所做的那样(你现在不是)
@interface ViewTestViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource> {
此外,请确保在创建时将self
设置为选择器视图委托和数据源:
//PICKER AREA
pickerList = [[NSMutableArray alloc] init];
[pickerList addObject:@"aaa"];
[pickerList addObject:@"bbb"];
CGRect pickerFrame = CGRectMake(0, 200, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
答案 1 :(得分:1)
在.h
文件中,您声明自己符合这些协议:
<UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource>
每个协议都涉及一些必须实现的方法(有关详细信息,请参阅每个方法的文档)。如果您没有实现所有必需的方法,您将收到此警告。