views:

164

answers:

2

I have a UIImageview in my application and I was wondering how I can show an image in it pragmatically and change it whenever I want to! I think my major problem is exactly WHERE to put it! If you could show me what code and where that would be very usefull! thanks!

EDIT: Also if you can pragmatically change the background into an image or even a color! thanks! either or will work!

EDIT2: here is my code, i dont know where to put those because they always give me an error!

MainView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
    //static int r;
    int r;
    IBOutlet UIButton *myButton;
    IBOutlet UILabel *myTextLabel;
    IBOutlet UILabel *myTextLabel2;
    IBOutlet UIImageView *myImage;

}

@property (nonatomic, retain) UIButton *myButton;
@property (nonatomic, retain) UILabel *myTextLabel;
@property (nonatomic, retain) UILabel *myTextLabel2;
@property (nonatomic, retain) UIImageView *myImage;



- (IBAction)buttonclick;

//+(void) initialize;

@end

and the

MainView.m

#import "MainView.h"
#include <stdlib.h>
#include <libc.h>


@implementation MainView

@synthesize myButton, myTextLabel, myTextLabel2, myImage;   


- (IBAction)buttonclick {

    r++;

    if(r == 1) {

     self.myTextLabel.text = @"word";
     self.myTextLabel2.text = @"def.";

    }else if(r == 2) {

     self.myTextLabel.text = @"word2";
     self.myTextLabel2.text = @"def2 "; 
    }

}

@end
+1  A: 

To change the image itself, you can write:

UIImage* myNewImage = ...;
myImageView.image = myNewImage;

To change the background of your UIImageView, you can write:

myImageView.backgroundColor = [UIColor redColor];

... or whatever other UIColor you would like, including another image used as a pattern.

fbrereto
hey i couldnt figure out where to put those without error! i posted my code above! could you please locate where for me?
You would put the code in the body of the method where you want to set the image and/or background color of the view.
Peter Hosey
This Worked! thanks!
A: 

If you implement the viewDidLoad() method in your view controller class you can set the image in the view there. When this method is automatically invoked all of your IBOutlets will be initialized already allowing you to set myImageView.image.

See viewDidLoad

James Wald