在UILabel上执行选择器会导致崩溃吗? [英] Performing selector on UILabel generates crash?

查看:87
本文介绍了在UILabel上执行选择器会导致崩溃吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到UILabels并不是为了响应触摸事件,而是我可以使用UIButton。但是,我必须继承子类UILabel来覆盖另一个方法,所以我想我也可以使用标签来保持对代码的更改。

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


推荐答案

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

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

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

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
  }
}

此外,手势识别器将调用具有多个状态的动作方法,类似于 -touchesBegan:... 系列方法中的那些状态。您应该在识别器处于适当状态时检查您是否只是在进行工作。对于简单的点击手势识别器,您可能只想在识别器处于 UIGestureRecognizerStateEnded 状态时才能工作(参见上面的示例)。有关手势识别器的详细信息,请参阅 UIGestureRecognizer

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.

这篇关于在UILabel上执行选择器会导致崩溃吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆