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 {
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 .
source share