-2

I am currently trying to make a signout button for an app.

I have the UIImageView "User Interaction Enabled" box checked.

in my .h file I have this:

@property (strong, nonatomic) IBOutlet UIImageView *signOutButtonIV;

in my .m file I have this:

- (void) viewDidLoad{

[super viewDidLoad];

    UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapResponder:)];

    tapGestureRecognizer.numberOfTapsRequired = 1;
    tapGestureRecognizer.numberOfTouchesRequired = 1;
    [self.signOutButtonIV addGestureRecognizer:tapGestureRecognizer];

}

-(void) tapResponder:(UITapGestureRecognizer *) sender{
    NSLog(@"single tap detected");
}

If I instead use [self addGestureRecognizer:tapGestureRecognizer] the taps will be detected. Is there a step I am missing?

Artem Novichkov
  • 2,356
  • 2
  • 24
  • 34
Airagale
  • 226
  • 1
  • 3
  • 19
  • 1
    Can you log `userInteractionEnabled` before adding gesture recognizer? – Artem Novichkov Dec 14 '16 at 17:32
  • 1
    is there any particular reason for using `UIImageView` instead of `UIButton` ? – Ratul Sharker Dec 14 '16 at 17:40
  • @ArtemNovichkov I checked and it was not enabled! I have the box checked in the story board under triats for "User Interaction Enabled". But that did not seem to work. Thank you – Airagale Dec 14 '16 at 18:05
  • @RatulSharker would it be ok to put an invisble button over an image view? I tried but I kept encountering an error "Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.' – Airagale Dec 14 '16 at 18:06
  • Actually i intend to use [setImage](https://developer.apple.com/reference/uikit/uibutton/1623997-setimage?language=objc) . The above error is saying you have added an IBOutlet / IBAction, but this not declared in the code implementation. – Ratul Sharker Dec 14 '16 at 18:19
  • Why use an imageView? I think you can use a button with an image background? then you can change the image based on the control – Joshua Dec 15 '16 at 02:12

3 Answers3

0

Add following lines

[self.signOutButtonIV setUserInteractionEnabled:YES]; 
[PARENT_VIEW bringSubviewToFront:self.signOutButtonIV];

before [self.signOutButtonIV addGestureRecognizer:tapGestureRecognizer];

Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
0

Thanks to Artem Novichkov pointed out to Log the userEnabledInteraction.

The log came back false. Even though i had checked the box in the storyboard file it was not enabled.

[signOutButtonIV setUserInteractionEnabled:YES];

was what was needed to fix my problem.

Thanks Artem!

Airagale
  • 226
  • 1
  • 3
  • 19
0

Can also use storyboard to achieve the results:-

enter image description here

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49