可点击的 UILabel

Clickable UILabel

我正在尝试制作一个 UILabel,如果我单击它就可以对其进行编辑。我有一个隐藏的文本字段。

根据这个答案:How to customize UILabel clickable

我尝试了以下方法:

- (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];
}

但是我在 didTapLabel 中的断点没有触发。故事板上的一切都已正确连接。

尝试在此处为标签启用用户交互,对我有用,请使用 UITapGesture 而不是 UIGesture 并且 Textfield 应该在 uilabel 下面,因为如果 textfield 在 label 上面,那么 uilabel 的用户交互将不会被考虑

使用此代码,成功了。已测试。

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");
}