0

I have tried setting up a simple UITapGestureRecognizer on a UILabel but it is not working. Nothing seems to be recognized and nothing is written to the log.

in a method called from viewDidLoad

  UILabel *miTN = [[UILabel alloc] initWithFrame:CGRectMake(300, 100, 150, 15)];
  [miTN setUserInteractionEnabled:YES];
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  [miTN addGestureRecognizer: tapRecognizer];
  [miView addSubview:miTN];

   ... later

  - (void)tapRecognized:(id)sender
  {
    NSLog(@"that tap was recognized");
  }

Also, this is called in response to an async network call. Could that be causing the problem? Is there some other issue that might be causing isssue? I'm not really sure what first step to debug would be - I checked Color Blended Layers to see if they were ovrelapping but don't appear to be.

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
timpone
  • 19,235
  • 36
  • 121
  • 211
  • What type of view is `miView`? Are you seeing the view on the screen? If it's an iPhone in portrait, you've got its origin horizontally aligned 20px from the right side of the screen. – sooper Feb 20 '14 at 21:06
  • it is just a UIView - one possible issue is that this code exists in a callback block but I think it shoudl be retained correclty. The label get written just fine in the simulator. – timpone Feb 20 '14 at 21:07
  • Are any other gestures being added to it or any parent views? – sooper Feb 20 '14 at 21:10
  • I don't think so - I am investigating that now. I was thinking it was just some stupid syntax mistake on my part originally but thinking now it's some kind of unanticipated interaction. – timpone Feb 20 '14 at 21:17

2 Answers2

1

You should add UIGestureRecognizerDelegate to the protocol list for your viewcontroller class

@interface TSViewController : UIViewController <UIGestureRecognizerDelegate>

and insert string:

tapRecognizer.delegate = self;

and replace

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];

to

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
stosha
  • 2,108
  • 2
  • 27
  • 29
  • Are you sure that's necessary? The OP isn't using any delegate methods. – sooper Feb 20 '14 at 20:55
  • you can try. i'm sure. – stosha Feb 20 '14 at 20:56
  • @sooper about delegate you can read http://stackoverflow.com/questions/11355671/how-do-i-implement-the-uitapgesturerecognizer-into-my-application – stosha Feb 21 '14 at 02:04
  • It would still work without setting the delegate and implementing the protocol as you're setting a target (`self`) for the action. Read the last comment posted for the solution of that question you've linked. – sooper Feb 21 '14 at 15:48
0

in Swift, you have to enable by doing this as follows.
yourLabel.isUserInteractionEnabled = true

Chiman Song
  • 141
  • 10