Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tranvutuan

macrumors member
Original poster
Dec 19, 2011
74
0
Hi everyone
First, Merry Christmas and Happy Holiday.....
My question is very simple :I have 4 images(UIIMAGEVIEW) and i would like to know which image has been touched
Please advice me.
Thanks
 
Please show us the relevant code you've written so we know where you need help.

(These forums need a bot that automatically asks for us...)
 
Please show us the relevant code you've written so we know where you need help.

(These forums need a bot that automatically asks for us...)
Ok let me make it all clear. I am having 4 images ( UIImageView).
Code:
MyImage.h

#import <UIKit/UIKit.h>

@interface ItemDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *myImg1;
@property (weak, nonatomic) IBOutlet UIImageView *myImg2;
@property (weak, nonatomic) IBOutlet UIImageView *myImg3;
@property (weak, nonatomic) IBOutlet UIImageView *myImg4;

@end
All I want is if i click on myImage1, it will show on the console that
Code:
myImg1 has just been touched
I used method below
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
However, it is used to detect a touch but it wont tell you which image has been touched.... Any ideas about this.
 
Hmmm... use UIButtons instead.

You're using a storyboard or xib file, right? Just use UIButtons, set the type to custom, and assign it the proper image. Visually, it'll look the same, but it'll be a lot easier to figure out which one was being touched.

Add a method like:
Code:
- (IBAction)image:(UIButton *)sender touchedWithEvent:(UIEvent *)event

and then connect your button within your storyboard or xib to the controller so that it calls at the appropriate time. (Probably for "Touch Down")

Within the method you can figure out which image sent it by checking what "sender" is.
 
Hmmm... use UIButtons instead.

You're using a storyboard or xib file, right? Just use UIButtons, set the type to custom, and assign it the proper image. Visually, it'll look the same, but it'll be a lot easier to figure out which one was being touched.

Add a method like:
Code:
- (IBAction)image:(UIButton *)sender touchedWithEvent:(UIEvent *)event

and then connect your button within your storyboard or xib to the controller so that it calls at the appropriate time. (Probably for "Touch Down")

Within the method you can figure out which image sent it by checking what "sender" is.

I am using StoryBoard. If switching to UIButton, there is no problem for me.However, I just want to know if there are any other mechanisms to detect which UIImageView has been touched though.
 
If you looked into the set touches (from the method you were originally using), you'd be able to find the location on the screen that was touched. You could then check whether the frame of each image contains the point that was touched. It'd be more difficult to program and probably wouldn't work as quickly as just using UIButtons.
 
Also, UIImageViews can't receive touches with your call afaik.
Because UIImageViews their basic property is "UserInteractionEnabled = NO"
So you need to set it to yes in your IB, or code it like that.
Second, like warfare said, i think it's better (if it's just 4 images), to use Custom UIButton's. And set an image to them. Then you can hook up the same method for each, but via the IBAction:(id)sender -> cast a new UIButton like this
Code:
UIButton *button = (UIButton*)sender;
then you can switch through your tags of the sender like this.
Code:
switch (sender.tag)
blalala/rest of switch case

And if you have set up the tags of your buttons like you wanted, you can play with those. And trigger seperate things based upon the button you want.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.