UIImage view questions
-
18-09-2019 - |
Question
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
Solution
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.
OTHER TIPS
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