Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing selector on UILabel generates crash?

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
like image 342
Mahir Avatar asked Dec 22 '22 02:12

Mahir


2 Answers

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.

like image 155
Jason Coco Avatar answered Jan 05 '23 15:01

Jason Coco


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

}

like image 31
sureshkumar Avatar answered Jan 05 '23 17:01

sureshkumar