views:

84

answers:

2

Hello, I want create a UIImageView with a button press. I did this with:

- (IBAction) addPicture:(id)sender {
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
    imageView.image = [UIImage imageNamed:@"Picture.png"];
    [self.view addSubview:imageView];
}

I "created" this imageView in the .h, because I want use the ImageView in other methods. How can I create unlimited UIImageViews in the methode with the IBAction which I can use all later for other methods and so on...Each imageView must get a name, or?

For example in my app-idea the user can create images with a button and then he select a picture with a touch and can move it and resize it.

I am familiar with the creating of UIImageViews, etc., because I've used so far only predefined UIImageViews (with IB) etc.. I hope for a helpful answer. Thanks

+3  A: 

UIView and its subclasses have tag property for this purpose:

 (IBAction) addPicture:(id)sender { 
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)]; 
    imageView.tag = 100;
    imageView.image = [UIImage imageNamed:@"Picture.png"]; 
    [self.view addSubview:imageView]; 
    [imageView release]; // Do not forget to release your object!
} 

After that you can access your UIImageView object everywhere in your view controller:

UIImageView* imView = (UIImageView*)[self.view viewWithTag:100];
Vladimir
Thanks, it works! But how can I detect, which UIImageView was selected (with a touch?) - Actually my code just gets the latest UIImageView and move it (with touchesBegan and so on).
Flocked
A: 

Perhaps you should do this

NSMuteableArray *arrayOfImageViews = [[NSMuteableArray alloc] init];

//when you are ready to create your image views
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 45.0, 324, 52.0)];
imageView.tag = x; //some unique number for this image view.
[arrayOfImageViews addObject:imageView];
[self.view addSubview:imageView];
[imageView release];

//you can then create the next view and so on

//to reference the view later you can do either one of these things
//where x is the index of the image view. 
UIImageView *imageView = (UIImageView *)[arrayOfImageViews objectAt:x];
jamone