Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mike Bronner
16,395 PointsUIImageView won't trigger TapGesture
I can't seem to get a tapgesture to trigger notifications for an uiimageview no matter what I try. Here is my code:
@interface ScanViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, retain) IBOutlet UIImageView *resultImage;
@property (retain, nonatomic) IBOutlet UITextField *resultText;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (strong, nonatomic) IBOutlet UIScrollView *scroller;
@property (strong, nonatomic) IBOutlet UIToolbar *ScanBar;
@property (strong, nonatomic) IBOutlet UIImageView *scanImage;
- (IBAction) scanButtonTapped;
@end
#import "ScanViewController.h"
#import "SearchResultsViewController.h"
#import "AppDelegate.h"
@interface ScanViewController ()
@property (nonatomic) float originalScrollerOffsetY;
- (void) keyboardWillShow: (NSNotification *) notification;
- (void) keyboardWillHide: (NSNotification *) notification;
- (void) dismissKeyboard: (id) sender;
- (void) startCamera: (id) sender;
@end
@implementation ScanViewController
@synthesize managedObjectContext = _managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) viewWillAppear:(BOOL)animated
{
[self.tabBarController setHidesBottomBarWhenPushed:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)]];
self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
//self.scroller.delegate = self;
//self.resultText.delegate = self;
[self.scanImage addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(startCamera)]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Camera Event Handling
- (void) startCamera: (id) sender
{
NSLog(@"anything");
}
2 Answers
Amit Bijlani
Treehouse Guest TeacherIf you want to add an IBAction then you should just use a button with an image. That's the easiest way to add an action using an image.
Mike Bronner
16,395 PointsHi Amit, thanks! Sometimes the most obvious answer is right in front of you.
I was making things a bit too complicated in my mind due to misunderstanding how iOS ImagePickerController works.
Thanks! :)