0

我创建了一个自定义 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];
    }];
}

现在,我只是不明白为什么我的自定义视图没有显示。

谢谢!

4

1 回答 1

2

The implementation of your CustomPicker class does not add any subviews to itself. Your custom picker view is properly being added and displayed but it is empty so there is nothing to see.

Update the initWithFrame: method of CustomPicker to add self.picker to self.

[self addSubview:self.picker];

BTW - your code to create and setup the UIPickerView should be done inside the if statement. You don't want to run that code if self is 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;
}
于 2013-08-26T21:40:37.747 回答