我创建了一个自定义UIView来保存一个选择器视图和一个带有几个按钮的导航栏。我还使用导航栏,2个按钮和选择器创建了一个自定义笔尖,所有这些都链接到我的头文件。我让它成为每个单独的类的委托,以处理所有的选择器视图委托。我的问题是它在需要时不会在屏幕上显示。我知道该方法被调用但自定义视图不会出现。以下是我的代码。
CustomPicker.m:
- (IBAction)selectClick:(id)sender
{
NSObject *obj = [self.list objectAtIndex:[self.picker selectedRowInComponent:0]];
[self.delegate pickerDidSelect:obj];
}
- (IBAction)selectCancel:(id)sender
{
[self.delegate removePickerFromView:self];
}
- (id)initWithFrame:(CGRect)frame
{
frame.size = CGSizeMake(320, 260);
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
self.picker = [[UIPickerView alloc] init];
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker.showsSelectionIndicator = YES;
return self;
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return [self.delegate numberOfComponentsInPicker:pickerView];
}
-(NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.delegate picker:pickerView numberOfRowsInComponent:component];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.delegate picker:pickerView titleForRow:row forComponent:component];
}
要求显示的方法
-(void)displayPicker
{
NSLog(@"display picker");
picker = [[CustomPicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 260)];
#warning picker set up incomplete
[self.view addSubview:picker];
CGRect frame = picker.frame;
frame.origin = CGPointMake(0, self.view.frame.size.height - 260);
[UIView animateWithDuration:1.5 animations:^{
[picker setFrame:frame];
}];
}
现在,我只是不明白为什么我的自定义视图没有显示。
谢谢!
答案 0 :(得分:2)
CustomPicker
类的实现不会向自身添加任何子视图。您的自定义选择器视图已正确添加并显示,但它是空的,因此无需查看。
更新initWithFrame:
CustomPicker
方法,将self.picker
添加到self
。
[self addSubview:self.picker];
顺便说一句 - 您创建和设置UIPickerView
的代码应该在if
语句中完成。如果self
为nil
,则您不想运行该代码。
- (id)initWithFrame:(CGRect)frame
{
frame.size = CGSizeMake(320, 260);
self = [super initWithFrame:frame];
if (self) {
self.picker = [[UIPickerView alloc] init];
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker.showsSelectionIndicator = YES;
[self addSubview:self.picker];
}
return self;
}