Performing selector on UILabel generates crash?

被刻印的时光 ゝ 提交于 2019-12-01 19:37:18

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.

//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];

}

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.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!