views:

111

answers:

2

Hello,

I am trying to work with a UIImageView programmatically and can't get it to appear on the screen. In my header I have the following lines:

@interface myClass : UIView {
    UIImageView *imageView;
}
@property (nonatomic, readonly) IBOutlet UIImageView *imageView;

Then in my viewWillAppear I have the following:

self.imageView.frame = CGRectMake(10, 10, 72, 72);
self.imageView.backgroundColor = [UIColor redColor];
[self.imageView setImage:[UIImage imageNamed:@"test-image.png"]];
[containerView addSubview:self.imageView];

And this doesn't work at all. If I add the following lines in viewWillAppear I can get a UIImageView to appear:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue_button_bg.png"]];
[containerView addSubview:imgView];

So I can't make sense of why instantiating a UIImageView would work but using the class's ivar would not. What am I doing wrong here?

Thank you very much!

A: 

Take the IBOutlet away and add:

self.imageView = [[ImageView alloc]...]
Tuomas Pelkonen
You can probably take the whole @property line also away if the image is only used in myClass.
Tuomas Pelkonen
A: 

You have to instantiate all objects. One way to do it, if you want to keep a reference to imageView around, is...(don't forget to release it in dealloc).

 imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue_button_bg.png"]];
[containerView addSubview:imageView];

You can work out the rest ;)

Jordan