Try this code.
//YourController.h @interface YourController : UIViewController { UIImageView* _photoImageView; } @property (retain, nonatomic) IBOutlet UIImageView *photoImageView; - (IBAction)changeImageAction:(id)sender; @end //YourController.m @synthesize photoImageView = _photoImageView; - (void)viewDidLoad { [super viewDidLoad]; self.photoImageView.image = [UIImage imageNamed:@"img.png"]; } - (IBAction)changeImageAction:(id)sender { self.photoImageView.image = [UIImage imageNamed:@"img_2.png"]; }
Some notes.
First of all, you must properly bind your photoImageView IBOutlet to Xcode. For testing purposes, I created a test button ( UIButton ) that is associated with IBAction . UIImageView and test button (I created both with IB) are childs from YourController .
Remember to import images into the project and free up memory in dealloc and in viewDidUnload .
- (dealloc) { [_photoImageView release]; _photoImageView = nil; [super dealloc] } - (void)viewDidUnload { [super viewDidUnload]; self.photoImageView = nil; }
Hope this helps.
Edit
The code is not ARC oriented, but with minor modifications, it can also be run using ARC.
source share