tags:

views:

28

answers:

2

Hi,

I am trying to make a button in my IPhone app which changes an existing image (simple, I know! but I've spent 2 days on this :)

I have a button:

- (IBAction)myButton {  
    myUIImageView.image = [UIImage imageNamed:@"image.jpg"];
    NSLog(@"my button");
}

I am attempting to find out why the button is not doing anything. "my button" does get logged to the console so I know its mapped correctly. Firstly, I was hoping someone could tell me if I could perhaps log some more important info here, like the image file path? Or perhaps you see why this isn't working.

I synthesize myUIImageView above this:

@synthesize myUIImageView;

And I declare it in my .h file:

- (IBAction)myButton;
    @property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;
+2  A: 

Typically, something is nil here.

You have

myUIImageView.image = [UIImage imageNamed:@"image.jpg"];

So the obvious suspects would be either:

  1. myUIImageView is nil, or
  2. [UIImage imagedNamed:@"image.jpg"] is returning nil.

Add some logging to see if either of those is true.

NSLog(@"imageView: %@, image resource: %@", myUIImageView, [UIImage imageNamed:@"image.jpg"]);

If the image isn't being loaded, then I would expect for you to see a change in the UI, so that's probably not the issue.

More likely is that the nib outlet for myUIImageView haven't been wired up correctly in Interface Builder, but the logging should point you in the right direction.

jabley
Forgive me, how do I log this? NSLog(@[UIImage imageNamed:@"image.jpg"]); doesn't work and NSLog([UIImage imageNamed:@"image.jpg"]); doesn't work either.
Pete Herbert Penito
NSLog(@"We have an image? %@", [UIImage imageNamed:@"foo.png"]);
jabley
+1  A: 

Two things to try:

  1. Make sure that your UIImageView is actually being displayed. Do do this, just set its background color to something visible, e.g., myUIImageView.backgroundColor = [UIColor redColor];.
  2. Make sure that your image file ("image.jpg") is part of your project.
James Huddleston
Ok well on number 2 image.jpg is definitely in the resources folder I can click on it and see it inside xcode. On number one, I have set a different image (image2.jpg) inside Interface Builder so that when the app first loads I can see the image2.jpg so I know its there, does that make sense?
Pete Herbert Penito
You're good on both counts. One more idea: Did you remember to connect up your myUIImageView outlet to your UIImageView in Interface Builder? You can verify this by logging the value of myUIImageView in your action method. The line would look like this: NSLog(@"%@", myUIImageView);
James Huddleston
Thanks for your help James, NSLog(@"%@", myUIImageView); returns (null) so I did what you said and it WORKS! Thank you so much man I can finally sleep tonight :)
Pete Herbert Penito
I'm happy to help.
James Huddleston