views:

106

answers:

1

I'm trying to subclass UIView to create a custom view that is essentially a container. The subclass contains a UILabel and a UIImageView- I can't make either of them show up. Here's an example of what I've been trying:

In my main view controller:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        ViewClass *myView = [[ViewClass alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
        [view addSubview:myView];
    }
    return self;
}

In my UIView subclass:

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UILabel *word = [[[UILabel alloc] initWithFrame:[self bounds]] autorelease];
        word.text = @"WHY ISN'T THIS SHOWING UP!?";
        [self addSubview:word];

    }
    return self;
}

Can anyone explain what I'm doing wrong? I'm way out of practice with UIKit and Obj-C, and I figure I'm probably missing something obvious.

A: 

If anyone stumbles across this unanswered question looking for help on a similar issue, here is the solution to my issue.

I was using [view addSubview:myView] when I should have been using (in this particular context) [self.view addSubview:myView]. My UIView subclass wasn't showing at all, which explained why none of its contents were showing.

dxq
How did that even compile?
sbwoodside
It's been so long, I don't remember exactly where this code was, but I imagine `view` was the (rather terse) name of another local object.
dxq