0
votes

I want to add an event to an image. but if i add this means it will gives the warning: UIImage maynot respond to addtarget action for control events. how can i correct this code.

this is my code UIImageView *image;

UIImage *image1=[UIImage imageNamed:@"left_arrow.png"]; image=[[UIImageView alloc]initWithImage:image1]; image.frame=CGRectMake(100,410,30,30);

[image1 addTarget:self action:@selector(buttonPressed2:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:image];
2

2 Answers

2
votes

If you don't want to use an extra button you can add a UITapGestureRecognizer to the imageView.

Like this:

UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed2:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[imageView setUserInteractionEnabled:YES];

but this is more like a UIControlEventTouchDown event

0
votes

You need to create a transparent custom UIButton on top of the UIImageView. The button will handle events. UIImageView can do that.

UIButton *btn  = [[[UIButton alloc] initWithFrame:imageView.frame] autorelease];
btn.buttonType=UIButtonTypeCustom;
[btn addTarget:...];