0

I've subclassed a UILabel and added 2 properties that look like this:

@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;

I then implemented UIView's touches began method like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    if ([target respondsToSelector:@selector(action)]) {
        [target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
    }
}

within the class containing the subclassed UILabel, i set the target and action like this:

    label.target = self;
    labek.action = @selector(myMethod);
    label.userInteractionEnabled = YES;

The class including the label does indeed have the method myMethod, and so it should respond to it. Any ideas why it might not?

thanks!

Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90

3 Answers3

1

You are setting your action like this label.action = @selector(myMethod); and then using action and passing it into a second selector [target respondsToSelector:@selector(action)]. That will not work.

You want to do this:

if ([target respondsToSelector:self.action]) {
    [target performSelector:self.action onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}

Basically, the if statement fails because the target does not respond to that selector. Therefore, it is never being called.

James Paolantonio
  • 2,194
  • 1
  • 15
  • 32
0

Do you get any errors in debug console?

How about

@selector(myMethod:) // with colon.

Selectors in Objective C

Community
  • 1
  • 1
coolnalu
  • 375
  • 1
  • 7
0

Be sure that the label is contained in the bounds of the superview

Andrea
  • 26,120
  • 10
  • 85
  • 131