views:

790

answers:

1

I'm trying to lay out some images in code using addSubview, and they are not showing up.

I created a class (myUIView) that subclasses UIView, and then changed the class of the nib file in IB to be myUIView.

Then I put in the following code, but am still getting a blank grey screen.

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
     [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.backgroundColor = [UIColor blackColor];

    UIImageView *black = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"black.png"]];
    black.center = self.center;
    black.opaque = YES;

    [self addSubview:black];

    [black release];
}
A: 

try implementing initWithCoder: sometimes I've had trouble with IB and initWithFrame:

or at least add a logging call to see if your init method is executed

cobbal
That worked.... Any idea why?
Craig
It's just the way IB does things, I don't know why really. A possibly better alternative is to use - (void)awakeFromNib which gets called after all the objects from the nib are loaded, so you can access IBOutlets and know that they have been initialized.
cobbal