UIPickerView 出现无法解释的警告
我在我的应用程序中实现了一个简单的 UIPickerView,并且不断收到警告,提示我无法解释不完整的实现和“协议中的方法未完成”。我浏览了一堆例子,但无法弄清楚我错过了什么。
是我的代码:
.h.m
@interface ViewTestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource> {
UIPickerView *pickerView;
NSMutableArray *pickerList;
....
}
@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *pickerList;
@end
这
- (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;
}
I implemented a simple UIPickerView in my app and I keep getting warnings that I can't explain about incomplete implementation and "method in protocol not completed". I went through a bunch of examples and could not figure out what am I missing.
Here is my code:
.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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要遵守 .h 中的
UIPickerViewDelegate
和UIPickerViewDataSource
,就像您对 UITableViewDelegate、UITableViewDataSource 等所做的那样(目前还没有)另外,请确保您设置 <创建时将 code>self 作为选取器视图委托和数据源:
You need to conform to the
UIPickerViewDelegate
andUIPickerViewDataSource
in your .h like you do for UITableViewDelegate, UITableViewDataSource etc (you're currently not)Also, make sure you are setting
self
as the picker view delegate and data source when you create it:在您的
.h
文件中,您声明自己遵守这些协议:每个协议都涉及一些您必须实现的方法(有关详细信息,请参阅每个协议的文档)。如果您没有实现所有必需的方法,您将收到此警告。
In your
.h
file you declare yourself as conforming to these protocols:Each of those protocols involves some methods you have to implement (see the documentation on each one for the details). If you don't implement all the required methods, you will get this warning.