Ios 为什么';我的动作没有响应选择器吗?
我对UILabel进行了子类化,并添加了两个如下所示的属性:Ios 为什么';我的动作没有响应选择器吗?,ios,uilabel,subclass,Ios,Uilabel,Subclass,我对UILabel进行了子类化,并添加了两个如下所示的属性: @property (nonatomic, assign) SEL action; @property (nonatomic, assign) id target; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([target respondsToSelector:@selector(action)]) { [t
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([target respondsToSelector:@selector(action)]) {
[target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
}
label.target = self;
labek.action = @selector(myMethod);
label.userInteractionEnabled = YES;
然后我实现了UIView的方法,如下所示:
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([target respondsToSelector:@selector(action)]) {
[target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
}
label.target = self;
labek.action = @selector(myMethod);
label.userInteractionEnabled = YES;
在包含子类UILabel的类中,我将目标和操作设置为:
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([target respondsToSelector:@selector(action)]) {
[target performSelector:@selector(action) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
}
label.target = self;
labek.action = @selector(myMethod);
label.userInteractionEnabled = YES;
包含标签的类确实有方法myMethod,因此它应该响应它。你知道为什么不会吗
谢谢 调试控制台中是否有错误 怎么样
@selector(myMethod:) // with colon.
确保标签包含在superview的边界中您正在这样设置操作
label.action=@selector(myMethod)
然后使用action并将其传递到第二个选择器[目标响应选择器:@selector(action)]
。那是行不通的
您要执行以下操作:
if ([target respondsToSelector:self.action]) {
[target performSelector:self.action onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
}
基本上,if语句失败是因为目标不响应该选择器。因此,它永远不会被调用