My idea is to add generated integer value to the name of UIImageView. Here is an example: I have three UIImageViews named imageView1, imageView2, imageView3. When a button is pressed I have there this simple code:
self.value = value + 1;
(and of course I have this "value" declared) Now I want add some code which says something like:
UIImage *img = [UIImage imageNamed:@"image.png"];
[imageView(value) setImage:img];
So this I want this part of code ...[imageView(value)... to use the value I defined above. How could I do this?
-
Um, use an array of UIImageViews perhaps ?Paul R– Paul R2012-09-29 09:01:24 +00:00Commented Sep 29, 2012 at 9:01
-
I'm sorry I don't know how to do so...TomasJ– TomasJ2012-09-29 09:04:16 +00:00Commented Sep 29, 2012 at 9:04
Add a comment
|
3 Answers
You can set a tag for your image views:
// set tag
[imageView1 setTag:1];
[imageView2 setTag:2];
//...
// get the image view with the tag
[(UIImageView *)[self.view viewWithTag:value] setImage:img];
//...
10 Comments
TomasJ
I tried it but nothing happen...no error but also no image shows. here is my code:
UIImage *img = [UIImage imageNamed:@"image.png"]; [(UIImageView *)[self.view viewWithTag:value] setImage:img];TomasJ
And in view did load I put
[imageView1 setTag:1]; [imageView2 setTag:2];Kjuly
@TomasJ what's the value of
cislotatku? It must be 1~3 if you set tags: 1, 2, 3 (only numeric is allowed).TomasJ
It's allways previous value + 1. In viewdidload it's declared as 0 so when button is first pressed it's 0+1 then again 1+1 etc
Kjuly
@TomasJ please put a NSLog for your value & image in button pressed method, check whether it is right or not.
|
Here I tried to did according to your requirement.
my ViewController Class
- (void)viewDidLoad
{
[super viewDidLoad];
ExtendedUIImageView *eimage=[[ExtendedUIImageView alloc]initExtendedUIImageViewWithFrame:CGRectMake(0, 0, 768, 1024)];
UIImage *img1=[UIImage imageNamed:@"mac.png"];
UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
UIImage *img3=[UIImage imageNamed:@"mac.png"];
[eimage addUIImage:img1 ToExtendedImageViewWithRect:CGRectMake(100, 100, 100, 100) withTag:1];
[eimage addUIImage:img2 ToExtendedImageViewWithRect:CGRectMake(210, 100, 100, 100) withTag:2];
[eimage addUIImage:img3 ToExtendedImageViewWithRect:CGRectMake(320, 100, 100, 100) withTag:3];
[self.view addSubview:eimage];
}
- (IBAction)chageImage:(UIButton *)sender {
NSLog(@"#####");
UIImage *img2=[UIImage imageNamed:@"images.jpeg"];
[eimage replaceImage:img2 forTag:3];
}
I Extended the UIView Class and created new Class ExtendedImageView class
ExtendedImageView.h
@interface ExtendedUIImageView : UIView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect;
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n;
-(void)replaceImage:(UIImage *)newImg forTag:(int)n;
@end
ExtendedImageView.m
@implementation ExtendedUIImageView
-(id)initExtendedUIImageViewWithFrame:(CGRect)rect{
if([super initWithFrame:rect]){
return self;
}
return nil;
}
-(void)addUIImage:(UIImage *)img ToExtendedImageViewWithRect:(CGRect)rect withTag:(int)n{
UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
//[imgView setBackgroundColor:[UIColor grayColor]];
imgView.frame=rect;
[imgView setTag:n];
[self addSubview:imgView];
}
-(void)replaceImage:(UIImage *)newImg forTag:(int)n{
for(UIView * img in [self subviews]){
if([img isKindOfClass:[UIImageView class]]&&[img tag]==n){
((UIImageView *)img).image=newImg;
}
}
}
@end