views:

220

answers:

2

Hello, The user can create a unlimited number of UIImageViews with a button press with this code:

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

So the first UIImageView gets the tag 1 and the second 2 and so on... Now how can I find out, which UIImageView was select by the user with a touch? I think, I have to do this in touchesBegan, but as I said, I don´t know how to get the right UIImageView.

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.

Thanks for your help.

+2  A: 

Instead of using tag of UIImageView, why don't you create your own subclass of UIImageView?
You can overwrite touchesBegan method in the subclass, so you can detect a touch.
Then in the subclass, you can move or resize a picture which the subclass has.

tomute
Sry, but can you explain it a little bit more? I´m very new in programming (4 weeks) and so also in objective-c. How can I create the own subclass and overwrite the touchesBegan-methode? Maybe some code would be helpful. Thanks.
Flocked
That's pretty easy. Following site would help you.http://forums.pragprog.com/forums/83/topics/1423
tomute
I tried it with the methode on pragprog, but it doesn´t work. It just works, when I make create a UIImageView with the interface Builder. Why?
Flocked
A: 

The SDK is fairly flexible; there are actually quite a number of ways to go about this:

Subclass UIImageView, as tomute has already mentioned. You would then respond to your UIImageView's touches in each object's touchesBegan, touchesMoved & touchesEnded methods.

If your UIImageViews don't handle the touch events then they'll be passed to the super view where you could check in the superview by asking

if ([[touch view] isKindOfClass:[UIImageView class]]

then perhaps

switch ([[touch view] tag])
case (1) {
…
break;
}
case (2) {
…
break;
}

The event will continue getting passed 'up' until something in the chain handles the touch.

Review Event Handling and the responder chain.

Meltemi