Reputation: 1
I am new in objective c.I have an image and i want to show some alert message when i click on it Added image like this-
AHolder = [[UIImageView alloc] initWithFrame:CGRectMake(5, 80, 40, 40)];
UIImage *imageA = [UIImage imageNamed:@"A.png"];
AHolder.image = imageA;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aPressed:) name:@"aPressed" object:nil];
[view addSubview:AHolder]
And its event like this-
-(IBAction)aPressed:(id)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
[alert show];
[alert release];
}
It doesn't give me any error but also when i click on the image nothing happens. Please suggest me any solution.
Upvotes: 0
Views: 979
Reputation: 12908
Need not to put UIButton
over your image. Just use UIButton
rather than UIImageView
. You can set UIButton
backgroundimage
or image
property to the image you want.
Other than this you can use UITapGestureRecognizer
like this:
Put this code to viewDidLoad
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[yourImage addGestureRecognizer:rec];
[yourImage setUserInteractionEnabled:YES]; //!! this is important
and implement this delegate method
#define unused_related_to_gesture
- (void)tapRecognized:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"\nUIGestureRecognizerStateBegan\n");
}
if(recognizer.state == UIGestureRecognizerStateCancelled)
{
NSLog(@"\nUIGestureRecognizerStateCancelled\n");
}
if(recognizer.state == UIGestureRecognizerStateChanged)
{
NSLog(@"\nUIGestureRecognizerStateChanged\n");
}
if(recognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"\nUIGestureRecognizerStateEnded\n");
}
if(recognizer.state == UIGestureRecognizerStateFailed)
{
NSLog(@"\nUIGestureRecognizerStateFailed\n");
}
if(recognizer.state == UIGestureRecognizerStatePossible)
{
NSLog(@"\nUIGestureRecognizerStatePossible\n");
}
if(recognizer.state == UIGestureRecognizerStateRecognized)
{
NSLog(@"\nUIGestureRecognizerStateRecognized\n");
// this section will contain your code of clicking the image.
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"hello" message:@"a pressed" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"ok",nil];
[alert show];
[alert release];
}
}
Upvotes: 0
Reputation: 398
Add "round rect button" object in the xib file. Set 'Type' attribute to 'custom', 'Background' attribute to 'nameofyourimage.extension'.
Connect aPressed: method to the touch up inside event of the round rect button.
Upvotes: 0
Reputation: 8460
Add tap gestures to the image view .
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture)];
tapGesture.numberOfTapsRequired=1;
[AHolder setUserInteractionEnabled:YES];
[AHolder addGestureRecognizer:tapGesture];
[tapGesture release];
-(void)handleTapGesture{
//do what ever you want here
}
Otherwise, simply use button with background image.
Upvotes: 2
Reputation: 1028
Very simple and most popular solution to your question would be to use a custom UIButton over your image. Then just set the selector as your method you want to call on click of button. Make sure that the frame of UIButton & UIImage is same.
Upvotes: 0
Reputation: 5343
Use tapGestureRecognizer. so whenever user taps the iamgeview, you can do what ever you want
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(aPressed)];
tapGesture.numberOfTapsRequired=1;
[aHolder setUserInteractionEnabled:YES];
[aHolder addGestureRecognizer:tapGesture];
-(void)aPressed{
//do what ever you want here
}
Upvotes: 0