我正在尝试制作一个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中的断点没有触发。一切都正确连接到情节提要上。
最佳答案
使用此代码,就可以了。经过测试。
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");
}