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
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With