【问题标题】:why doesn't my action respond to the selector?为什么我的操作没有响应选择器?
【发布时间】:2012-09-06 18:31:12
【问题描述】:

我对 UILabel 进行了子类化并添加了 2 个如下所示的属性:

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

然后我实现了 UIView 的 touches started 方法,如下所示:

- (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,所以它应该响应它。任何想法为什么它可能不会?

谢谢!

【问题讨论】:

    标签: ios uilabel subclass


    【解决方案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 语句失败是因为目标没有响应该选择器。因此,它永远不会被调用。

    【讨论】:

      【解决方案2】:

      调试控制台有错误吗?

      怎么样

      @selector(myMethod:) // with colon.
      

      Selectors in Objective C

      【讨论】:

        【解决方案3】:

        确保标签包含在父视图的边界内

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多