UILabel をサブクラス化し、次のような 2 つのプロパティを追加しました。
@property (nonatomic, assign) SEL action;
@property (nonatomic, assign) id target;
次に、UIView の touches begin メソッドを次のように実装しました。
- (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 があるため、それに応答する必要があります。なぜそれができないのでしょうか?
ありがとう!