0

I am trying to make a UILabel that if I click I can edit it. I have a hidden textfield.

Per this answer: How to customize UILabel clickable

I tried the following:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    tapInput = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(didTapLabel)];
    [lblInput setUserInteractionEnabled:YES];
    [lblInput addGestureRecognizer:tapInput];
}

-(void)didTapLabel
{
    [txtHidden becomeFirstResponder];
}

But my break point inside didTapLabel is not firing. Everything is hooked up correctly on the storyboard.

JoshDG
  • 3,871
  • 10
  • 51
  • 85
  • 1
    `UITapGestureRecognizer` vs `UIGestureRecognizer`. `UIGestureRecognizer` is quite "generic", and you didn't set any number of taps required, etc... – Larme Dec 08 '17 at 09:15
  • Have you thought about only using a UITextField alone and doing isUserInteractionEnabled = false and endEditing: on it to make it uneditable? – uliwitness Dec 08 '17 at 19:13

2 Answers2

1

Try enabling user interaction here for label,Worked for me, and please use UITapGesture instead of UIGesture And Textfield should be below uilabel because if the textfield is above label then uilabel's user interaction won't be considered

Enable User interaction

Hitesh Sultaniya
  • 929
  • 1
  • 7
  • 12
1

Use this code, It's worked. Tested.

UILabel * clickMeLbl=[[UILabel alloc] initWithFrame:CGRectMake(75,30,170,30)];
clickMeLbl.text = @"Click Me";
clickMeLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size: 18.0f ];
clickMeLbl.textAlignment =  NSTextAlignmentCenter;
clickMeLbl.textColor = [UIColor blackColor];
[self.view addSubview:clickMeLbl];

clickMeLbl.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapLabelWithGesture:)];
[clickMeLbl addGestureRecognizer:tapGesture];


- (void)didTapLabelWithGesture:(UITapGestureRecognizer *)tapGesture {
   NSLog(@"Click Me Label Clicked");
}
Mukesh
  • 777
  • 7
  • 20