自定义按钮和目标返回值

时间:2016-02-19 15:51:05

标签: objective-c uigesturerecognizer

我创建了一个自定义Button类。我有一个UITapGestureRecognizer来识别水龙头并根据需要做出响应:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.cancelsTouchesInView = YES;
self.recognizer = recognizer;
[buttonView addGestureRecognizer:recognizer];

我创建按钮并添加目标:

[buttonList addTarget:self action:@selector(buttonListSelected)];

-(void)addTarget:(id)target action:(SEL)action {
    [self.recognizer addTarget:target action:action];
}

当我创建action方法并要求它返回类时:

-(void)buttonSortSelected:(SubButton *)subButton {}

返回UITapGestureRecognizer

我可以进行设置,以便自定义类会返回self而不是识别器吗?

1 个答案:

答案 0 :(得分:1)

如果您想自己更改必须处理tapDetected:的参数。

// In your custom button class

-(void)addTarget:(id)target action:(SEL)action {
    self.target = target;
    self.targetedSelector = selector;
}

然后

// In your custom button class

- (void)tapDetected:(UITapGestureRecognizer *)recognizer {
   [self.target performSelector:self.targetedSelector withObject:self];
}