0

I add a tap gesture on my label but I couldn't retrieve the tapped label in the selector function. I mean, for example, my function is like this:

-(void)changeLabel:(UIGestureRecognizer *)sender{}

If this gesture is added on a imageview, I can get the imageview by using sender.view.
But since it's a uilabel I don't know to get it and then change text content.

Hope my question is clear...

hgwhittle
  • 9,316
  • 6
  • 48
  • 60
Violet
  • 25
  • 1
  • 5
  • Huh? Isn't an `UILabel` a view too? –  Aug 09 '13 at 20:00
  • using `UIButtonView` would be great since buttons are made for tap and any such events while `UILabelView` is to just show a text i.e to notify user. – Ayush Aug 09 '13 at 20:21

4 Answers4

2

Set the interaction to enabled, then add it as you normally would.

My example:

- (void)viewDidLoad
 {
    [super viewDidLoad];
    self.mainLabel.text = @"This is my label";
    UITapGestureRecognizer *tapRecongizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeText)];
    self.mainLabel.userInteractionEnabled = YES;
    [self.mainLabel addGestureRecognizer:tapRecongizer];
}

-(void)changeText
{
    NSArray *randomNames = @[@"Jim", @"Bob", @"John", @"Peter"];
    self.mainLabel.text = [randomNames objectAtIndex:arc4random() % 4];
}
Peter Foti
  • 5,526
  • 6
  • 34
  • 47
1

Set your UILabel's userInteractionEnabled property to YES. I believe this property is defaulted to NO on new instances of UILabel.

self.yourLabel.userInteractionEnabled = YES;
hgwhittle
  • 9,316
  • 6
  • 48
  • 60
0

Add a clear button to on top of your UILabel with an identical frame

Then create an IBAction for the button touch, and handle changing the labels text in there.

RyanG
  • 4,393
  • 2
  • 39
  • 64
  • This would be a bit of a hack. There are systems in place to do this correctly, using the way the system is intended to work. – tooluser Jul 16 '14 at 17:19
0

You could add a UIButton above the label that has the same frame as the label and very low opacity, and have the button perform the action.

user2666713
  • 200
  • 9
  • This would be a bit of a hack. There are systems in place to do this correctly, using the way the system is intended to work. – tooluser Jul 16 '14 at 17:18