UIPickerView 出现无法解释的警告

发布于 2024-11-30 19:36:13 字数 1650 浏览 4 评论 0原文

我在我的应用程序中实现了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

本王不退位尔等都是臣 2024-12-07 19:36:13

您需要遵守 .h 中的 UIPickerViewDelegateUIPickerViewDataSource,就像您对 UITableViewDelegate、UITableViewDataSource 等所做的那样(目前还没有)

@interface ViewTestViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource> {

另外,请确保您设置 <创建时将 code>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;

You need to conform to the UIPickerViewDelegate and UIPickerViewDataSource in your .h like you do for UITableViewDelegate, UITableViewDataSource etc (you're currently not)

@interface ViewTestViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource> {

Also, make sure you are setting self as the picker view delegate and data source when you create it:

//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;
杀手六號 2024-12-07 19:36:13

在您的 .h 文件中,您声明自己遵守这些协议:

<UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource>

每个协议都涉及一些您必须实现的方法(有关详细信息,请参阅每个协议的文档)。如果您没有实现所有必需的方法,您将收到此警告。

In your .h file you declare yourself as conforming to these protocols:

<UITableViewDelegate, UITableViewDataSource, UIPageViewControllerDataSource, UIPageViewControllerDataSource>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文