Reputation: 14418
I would like to fire up this event:
- (IBAction)profilePop:(id)sender
{
ProfileViewController * profile = [[ProfileViewController alloc] init];
UIImageView * temp = ((UIImageView *)sender);
profile.uid = [[[posts objectAtIndex:((UIImageView *)sender).tag] creator] mid];
NSLog(@"profile id %@", profile.uid);
UIPopoverController * profilePop = [[UIPopoverController alloc] initWithContentViewController:profile];
[profilePop presentPopoverFromRect:CGRectMake(temp.frame.origin.x+temp.frame.size.width, temp.frame.origin.y + temp.frame.size.height/2, profile.view.frame.size.width, profile.view.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
when a user taps on an UIImageView. All I am trying to do is to show a popover when an UIImageView is clicked and it is shown to the right of the UIImageView. I see that UIImageView doesn't have an addAction attribute from it as it's not a subclass of UIControl. I did some research that I might probably have to use a UIButton instead. Is this true? Is there a way to do this using UIImageView so I don't have to rewrite the code again? I
Upvotes: 0
Views: 1025
Reputation: 31722
First.
you could get the touch on any object, which has super class as UIView
.
if you see the UIImageView in apple documentation.
UIView : UIResponder : NSObject
UIResponder has function to get the touches. So implement the below functions in your view class and detect the touches on your UIImageView
.
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
Second:
you could also create the UITapGestureRecognizer
for UIImageView
.
Check the below blog tutorial.
Working with UIGestureRecognizers
EDITED:
Use below code :
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[MyImageView addGestureRecognizer:tapRecognizer];
if user tap once, tapped function will be called , So you tabbed function implemntation should be look like below
-(void)tapped:(id)sender
{
NSLog(@"See a tap gesture");
ProfileViewController * profile = [[ProfileViewController alloc] init];
UIImageView * temp = [(UIPanGestureRecognizer*)sender view];
profile.uid = [[[posts objectAtIndex:((UIImageView *)sender).tag] creator] mid];
NSLog(@"profile id %@", profile.uid);
UIPopoverController * profilePop = [[UIPopoverController alloc] initWithContentViewController:profile];
[profilePop presentPopoverFromRect:CGRectMake(temp.frame.origin.x+temp.frame.size.width, temp.frame.origin.y + temp.frame.size.height/2, profile.view.frame.size.width, profile.view.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}
Upvotes: 2
Reputation: 44633
You could create a Tap Gesture recognizer and attach it to the UIImageView.
// By now imageView exists!
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
Now the handleTapGesture
will be quite similar to your -profilePop:
Since you are talking of pop over controllers, I assume you already have gesture recognizers available.
Upvotes: 0
Reputation: 12106
You can define a UIButton of type custom, don't give it any text or image, give it a clear background, and the same frame as your image view. Then add your target/selector to the button and it will appear to the user they are tapping the image view, when they are actually tapping an invisible button. This should take about 3 minutes to set up in IB, so I don't think you'll need to rewrite your code again.
Upvotes: 0
Reputation: 36627
What about using an UIButton instead and using the class method to add your UIImage to it.
- (void)setImage:(UIImage *)image forState:(UIControlState)state
Upvotes: 0