简体   繁体   English

UIPickerView无法解释的警告

[英]unexplained warnings with UIPickerView

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". 我在应用程序中实现了一个简单的UIPickerView,并且不断收到警告,提示我无法解释未完成的实现和“协议中的方法未完成”。 I went through a bunch of examples and could not figure out what am I missing. 我列举了很多例子,但无法弄清我想念的是什么。

Here is my code: 这是我的代码:

.h 。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 .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;
}

You need to conform to the UIPickerViewDelegate and UIPickerViewDataSource in your .h like you do for UITableViewDelegate, UITableViewDataSource etc (you're currently not) 您需要遵循.h中的UIPickerViewDelegateUIPickerViewDataSource ,就像您对UITableViewDelegate,UITableViewDataSource等所做的一样(当前不是)

@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: 另外,请确保在创建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;

In your .h file you declare yourself as conforming to these protocols: 在您的.h文件中,您声明自己符合以下协议:

<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. 如果未实现所有必需的方法,则会收到此警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM