Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Slocum

macrumors newbie
Original poster
Feb 7, 2010
2
0
I can get UIImageView to display the image, if I set the image in the Interface Builder. But nothing shows when I set the parameters programmatically.

Here is the code:

ImageTestViewController.h:

Code:
#import <UIKit/UIKit.h>

@interface ImageTestViewController : UIViewController {
	UIImageView *imageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@end

ImageTestViewController.m:

Code:
#import "ImageTestViewController.h"

@implementation ImageTestViewController

@synthesize imageView;

- (void)viewDidLoad {
	UIImage *image = [UIImage imageNamed:@"apress_logo.png"];
	imageView = [[UIImageView alloc] initWithImage:image];
    [super viewDidLoad];
}

- (void)viewDidUnload {
	imageView = nil;
	[super viewDidUnload];
}

- (void)dealloc {
	[imageView release];
    [super dealloc];
}

@end

I connected the UIImageView in the interface to the IBOutlet. The image is in the Resources. I still won't display. What have I done wrong?

Thanks.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I am confused. If you have connected your IBOutlet to a UIImageView instance in Interface Builder then why are you creating a new UIImageView in your init? You are not altering the properties of the UIImageView you created in IB by doing this. You should be setting the image property of the instance you already have.

If you want to create your own UIImageView (if you do this delete then one in IB) then you should be adding it to a visible view hierarchy. The UIImageView you create is never added to a view that's on a visible window so will never be seen.
 

Slocum

macrumors newbie
Original poster
Feb 7, 2010
2
0
Thank you. Boneheaded mistake.

I've changed it to:

Code:
- (void)viewDidLoad {
	UIImage *image = [UIImage imageNamed:@"apress_logo.png"];
	imageView.image = image;
    [super viewDidLoad];
}

and it works.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.