views:

48

answers:

2

Hi!

I'm a beginner to Iphone Development :)

I am trying to make a button change an image. So I have my button

- (IBAction)myButton { 
    myUIImageView.image = [UIImage imageNamed:@"myPhoto.png"];
}

I created a UIImageView in IB and I named the label and the name of this 'myUIImageView' but XCode is telling me that it's undeclared. So my question is how do I associate this UIImageView with myUIImageView. Or perhaps how do I reference this UIImage in my myButton IBAction?

Any advice would help, thanks alot!

+2  A: 

In your .h, you need this ivar between the curly braces

UIImageView* myUIImageView;

And after the close and before the @end, you need

@property(retain, nonatomic) IBOutlet UIImageView* myUIImageView;

and in the .m, after the @implementation line

@synthesize myUIImageView;

(release in your dealloc and viewDidUnload)

Now,

  1. Open up Interface Builder for the .xib for this view controller
  2. Click on File's Owner icon in the Document dialog
  3. Bring up the Inspector
  4. Go to the connections tab
  5. You should see an outlet named myUIImageView with a circle next to it
  6. Click and drag the circle to the UIImageView in your view (this connects the outlet to the view)
  7. Save, close, rebuild
Lou Franco
Thank you so much man, this really helped me!
Pete Herbert Penito
A: 

how to connect the UIImageView in interface builder with the outlet created in xcode named myUIImageView: close interface builder and open up xcode. Heres what you need to write correctly in the following two files.

in XCode

.h file

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

.m file

after implementation you write

@synthesize myUIImageView;

release in your dealloc and viewDidUnload.

save the file in xcode then open up the xib file that is connected to the .h and .m files. For example i have firstViewcontroller.h and firstViewController.m then i have a .xib file called firstView.xib.

in Interface Builder

Now on the view drag a UIImageView and then in the document dialog you will see the file owners icon. click on that and press CMD+2 to open up the inspector. Go to the connections tab and there will be an outlet named myUIImageView that we created in xcode. next to it is a circle which you click and drag to your UIImageView. This will connect the outlet in xcode with the imageview in interface builder. Now save the file. close interface builder and rebuild your project.

Thats the first question answered once you ellaborated on question two i will help you with that.

Let me know if you need any more help.

PK

Pavan