2012-09-06 31 views
0

我子类一个UILabel并添加2个属性看起来像这样:为什么我的动作不会响应选择器?

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

我再实施的UIView的接触开始的方法是这样的:在类中

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

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

包含的子类的UILabel,我设置目标和动作是这样的:

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

包含该标签的类确实具有方法myMethod,因此它显示uld回应它。任何想法,为什么它可能不?

谢谢!

回答

1

你设置你的动作像这样label.action = @selector(myMethod);,然后用行动并将其传递的边界进入第二个选择器[target respondsToSelector:@selector(action)]。这是行不通的。

你想这样做:

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

基本上,因为目标不给回应选择if语句失败。因此,它永远不会被调用。

+0

真棒作品像一个魅力谢谢 –

0

确保标签包含在上海华

相关问题