Do selector execution in UILabel lead to failure?

I read that UILabels are not designed to respond to touch events and that I can just use UIButton. However, I have to subclass UILabel anyway in order to override another method, so I thought I could use a shortcut to make minimal changes to my code.

How can I get my label to respond to touch events? Below is the code and error.

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 
+4
source share
4 answers

Since UILabel not a control, you cannot send the message -addTarget:action:forControlEvents: You must remove this line from your application as your shortcut is not a control and will never reply to this message. Instead, if you want to use your shortcut, you can install it interactively 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 to the instance of the gesture recognizer that called it, and not the control, for example, an action message. To retrieve the instance of the label that caused the event, pass the recognized attribute using -view . So, if your updateLabel: method can be implemented as follows:

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

In addition, the gesture recognizer will call a multi-state action method similar to those found in the -touchesBegan:... methods. You must verify that you are only doing work while the recognizer is in the appropriate state. For your simple gesture recognizer, click, you probably only want to work when the recognizer is in the UIGestureRecognizerStateEnded state (see Example above). For more information about gesture recognizers, see the Documentation for UIGestureRecognizer .

+9
source

// create shortcut

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

// NAVIGATION IN HOMOGENDS IN ANOTHER VIEW

- (void) tapAction // METHOD TO ADD IT TO ROUTING OPERATION

{

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

}

+1
source

The best part is to use UIButton to accomplish what you are trying to do.

But if you really want to subclass UILabel , be sure to set userInteractionEnabled to YES.

The documentation says :

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

And addTarget: action: forControlEvents: will not work because UILabel does not come from UIControl . In one place, you can catch your events by applying the UIResponder touchesBegan:withEvent: method in your subclass.

0
source

Here is the quick version 2.1 for UILabel click

 let label = UILabel(frameSize) let gesture = UITapGestureRecognizer(target: self, action: "labelTapped:") labelHaveAccount.userInteractionEnabled = true labelHaveAccount.addGestureRecognizer(gesture) func labelTapped(gesture:UIGestureRecognizer!){ //lable tapped } 
0
source

Source: https://habr.com/ru/post/1393675/


All Articles