Matt S.
Matt S.

Reputation: 13753

iPhone SDK: detect view that was tapped

In my application I have a lot of little UIImageViews and a view off to the side. What I need is that view off to the side to be able to detect which image view was tapped and give me all the information about it (much like with (id)sender) as well as its coordinates. What is the best way to do this?

Upvotes: 0

Views: 3248

Answers (3)

k-thorat
k-thorat

Reputation: 5123

Add tag to UIImageView like myUIImageView.tag = intNumber; and in side touchesBegan or you can call any common method for all your UIImageView and use tag to identify which view is tapped.

Upvotes: 0

Max
Max

Reputation: 16719

This is the internal class from my project. I think you get the idea. As an option you can create protocol for the owner (or delegate). You can obtain coords using

-[UITouch locationInView: someView]

Here is the code:

@interface _FaceSelectView : UIImageView {
@protected
    VIFaceSelectVC* _owner;
}
-(id) initWithOwner:(FaceSelectVC*) owner;
@end

@implementation _FaceSelectView

-(id) initWithOwner:(FaceSelectVC*) owner {
    if( self = [super init] ) {
        _owner = owner;
        self.userInteractionEnabled = YES;
    }

    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_owner _touchesBeganIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesEndedIn: self withTouch: (UITouch*)[touches anyObject]];
}

-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event {
    [_owner _touchesMovedIn: self withTouch: (UITouch*)[touches anyObject]];
}

@end

Upvotes: 2

GorillaPatch
GorillaPatch

Reputation: 5057

I would subclass UIImageView and add a new property for the target view you would like to message. Then reenable userInteractionEnabled and add a action to touchesUpInside.

In the action method of the custom subclass call a method on the target view in which you also give the object of the custom subview. Basically you use delegation and pass in the delegate call all parameters you need.

Upvotes: 1

Related Questions