在 UILabel 上执行选择器会导致崩溃?

发布于 2024-12-29 10:37:50 字数 552 浏览 1 评论 0原文

我读到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み青杉依旧 2025-01-05 10:37:50

由于 UILabel 不是控件,因此您无法发送 -addTarget:action:forControlEvents: 消息。您必须从应用程序中删除该行,因为您的标签不是控件并且永远不会响应该消息。相反,如果您想使用标签,您可以将其设置为交互式并向其添加手势识别器:

// label setup code omitted
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateLabel:)];
[tempLabel setUserInteractionEnabled:YES];
[tempLabel addGestureRecognizer:tap];
[tap release]; // if not using ARC

手势识别器的回调将传递触发它的手势识别器的实例,而不是像操作消息那样的控件。要获取触发事件的标签实例,请使用 -view 向传入的手势识别器发送消息。因此,如果您的 updateLabel: 方法可能按如下方式实现:

- (void)updateLabel:(UIGestureRecognizer*)recognizer
{
  // Only respond if we're in the ended state (similar to touchupinside)
  if( [recognizer state] == UIGestureRecognizerStateEnded ) {
    // the label that was tapped
    UILabel* label = (UILabel*)[recognizer view];

    // do things with your label
  }
}

此外,手势识别器将调用具有多个状态的操作方法,类似于 -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:

// label setup code omitted
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateLabel:)];
[tempLabel setUserInteractionEnabled:YES];
[tempLabel addGestureRecognizer:tap];
[tap release]; // if not using ARC

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 your updateLabel: method might be implemented as below:

- (void)updateLabel:(UIGestureRecognizer*)recognizer
{
  // Only respond if we're in the ended state (similar to touchupinside)
  if( [recognizer state] == UIGestureRecognizerStateEnded ) {
    // the label that was tapped
    UILabel* label = (UILabel*)[recognizer view];

    // do things with your label
  }
}

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 the UIGestureRecognizerStateEnded state (see the example above). For more information on gesture recognizers, see the documentation for UIGestureRecognizer.

忆依然 2025-01-05 10:37:50

//创建标签

_label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-75,self.view.frame.size.height-60, 150, 50)];

_label.backgroundColor = [UIColor clearColor];
_label.textColor=[UIColor whiteColor];
_label.text = @"Forgot password ?";
UITapGestureRecognizer *recongniser = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];//ADD ACTION TO LABEL
[_label setUserInteractionEnabled:YES];
[_label addGestureRecognizer:recongniser];

//导航到一个视图到另一个视图

-(void) tapAction //将其添加到标签选择器的方法

{

_forgotviewController=[[ForgotPassword alloc]init];
[self.navigationController pushViewController:self.forgotviewController animated:YES];

}

//create label

_label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.center.x-75,self.view.frame.size.height-60, 150, 50)];

_label.backgroundColor = [UIColor clearColor];
_label.textColor=[UIColor whiteColor];
_label.text = @"Forgot password ?";
UITapGestureRecognizer *recongniser = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];//ADD ACTION TO LABEL
[_label setUserInteractionEnabled:YES];
[_label addGestureRecognizer:recongniser];

//NAVIGATE TO ONEVIEW TO ANOTHER VIEW

-(void) tapAction //METHOD TO ADD IT TO LABEL SELECTOR

{

_forgotviewController=[[ForgotPassword alloc]init];
[self.navigationController pushViewController:self.forgotviewController animated:YES];

}

失而复得 2025-01-05 10:37:50

这里最明智的做法是使用 UIButton 来完成您想要做的事情。

但如果您确实想要子类化 UILabel,请确保将 userInteractionEnabled 设置为 YES。

文档说

新标签对象默认配置为忽略用户事件。
如果你想在 UILabel 的自定义子类中处理事件,你必须
将 userInteractionEnabled 属性的值显式更改为
初始化对象后是的。

并且 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 set userInteractionEnabled to YES.

The documentation says:

New label objects are configured to disregard user events by default.
If you want to handle events in a custom subclass of UILabel, you must
explicitly change the value of the userInteractionEnabled property to
YES after initializing the object.

And addTarget: action: forControlEvents: wouldn't work, because UILabel isn't descended from UIControl. One place you can catch your events by implementing UIResponder's touchesBegan:withEvent: method in your subclass.

行至春深 2025-01-05 10:37:50

这是 UILabel 点击的 swift 2.1 版本

let label = UILabel(frameSize)

let gesture = UITapGestureRecognizer(target: self, action: "labelTapped:")
labelHaveAccount.userInteractionEnabled = true
labelHaveAccount.addGestureRecognizer(gesture)

func labelTapped(gesture:UIGestureRecognizer!){
//lable tapped
}

Here is swift 2.1 version for UILabel tap

let label = UILabel(frameSize)

let gesture = UITapGestureRecognizer(target: self, action: "labelTapped:")
labelHaveAccount.userInteractionEnabled = true
labelHaveAccount.addGestureRecognizer(gesture)

func labelTapped(gesture:UIGestureRecognizer!){
//lable tapped
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文