在 UILabel 上执行选择器会导致崩溃?
我读到 UILabels 并不意味着响应触摸事件,并且我可以只使用 UIButton。然而,我无论如何都必须子类化 UILabel 来重写另一个方法,所以我想我也可以使用标签来尽量减少对代码的更改。
如何让我的标签响应触摸事件?显示的代码和错误如下。
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(startingPoint, 5, 10, 22)];
tempLabel.text = equationText;
tempLabel.font = [UIFont systemFontOfSize:13];
[tempLabel sizeToFit];
[view addSubview:tempLabel];
[tempLabel addTarget:self action:@selector(updateLabel:) forControlEvents:UIControlEventTouchUpInside]; // UNRECOGNIZED SELECTOR SENT TO INSTANCE
I read that UILabels aren't meant to respond to touch events, and that I could just use a UIButton. However, I have to subclass UILabel anyways to override another method, so I thought I might as well use a label to keep changes to my code a minimum.
How can I get my label to respond to touch events? The code and error displayed are below.
UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(startingPoint, 5, 10, 22)];
tempLabel.text = equationText;
tempLabel.font = [UIFont systemFontOfSize:13];
[tempLabel sizeToFit];
[view addSubview:tempLabel];
[tempLabel addTarget:self action:@selector(updateLabel:) forControlEvents:UIControlEventTouchUpInside]; // UNRECOGNIZED SELECTOR SENT TO INSTANCE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
UILabel
不是控件,因此您无法发送-addTarget:action:forControlEvents:
消息。您必须从应用程序中删除该行,因为您的标签不是控件并且永远不会响应该消息。相反,如果您想使用标签,您可以将其设置为交互式并向其添加手势识别器:手势识别器的回调将传递触发它的手势识别器的实例,而不是像操作消息那样的控件。要获取触发事件的标签实例,请使用
-view
向传入的手势识别器发送消息。因此,如果您的updateLabel:
方法可能按如下方式实现:此外,手势识别器将调用具有多个状态的操作方法,类似于
-touchesBegan:...< /code> 系列方法。您应该检查您是否仅在识别器处于适当状态时才提交工作。对于简单的点击手势识别器,您可能只想在识别器处于
UIGestureRecognizerStateEnded
状态时进行工作(请参阅上面的示例)。有关手势识别器的更多信息,请参阅 UIGestureRecognizer。Since
UILabel
isn't a control, you can't send the-addTarget:action:forControlEvents:
message. You must remove that line from your application since your label is not a control and will never respond to that message. Instead, if you wanted to use your label, you could set it interactive and add a gesture recognizer to it:The callback for the gesture recognizer will be passed the instance of the gesture recognizer that triggered it, not the control like an action message would. To get the instance of the label that triggered the event, message the passed-in gesture recognizer with
-view
. So, if yourupdateLabel:
method might be implemented as below:Also, the gesture recognizer will call the action method with multiple states, similar to those found in the
-touchesBegan:...
series of methods. You should check that you're only committing work while the recognizer is in the appropriate state. For your simple tap gesture recognizer, you probably only want to do work when the recognizer is in theUIGestureRecognizerStateEnded
state (see the example above). For more information on gesture recognizers, see the documentation for UIGestureRecognizer.//创建标签
_label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-75,self.view.frame.size.height-60, 150, 50)];
//导航到一个视图到另一个视图
-(void) tapAction //将其添加到标签选择器的方法
{
}
//create label
_label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-75,self.view.frame.size.height-60, 150, 50)];
//NAVIGATE TO ONEVIEW TO ANOTHER VIEW
-(void) tapAction //METHOD TO ADD IT TO LABEL SELECTOR
{
}
这里最明智的做法是使用 UIButton 来完成您想要做的事情。
但如果您确实想要子类化
UILabel
,请确保将userInteractionEnabled
设置为 YES。文档说:
并且
addTarget: action: forControlEvents:
不起作用,因为UILabel
不是UIControl
的后代。您可以通过实施 UIResponder 的touchesBegan:withEvent:
方法在你的子类中。Smartest thing to do here is to use a UIButton to do what you are trying to do.
But if you really want to subclass
UILabel
, make sure to setuserInteractionEnabled
to YES.The documentation says:
And
addTarget: action: forControlEvents:
wouldn't work, becauseUILabel
isn't descended fromUIControl
. One place you can catch your events by implementing UIResponder'stouchesBegan:withEvent:
method in your subclass.这是 UILabel 点击的 swift 2.1 版本
Here is swift 2.1 version for UILabel tap