我正在尝试将以下代码精简为一个方法。目前,八种相同的方法用于处理对八个UIButton
实例的点击。理想情况下,我只想将UIButton
作为参数传递给,@selector
这样我就可以避免需要八个相同的方法。我正在努力将参数传递给选择器的语法。如果可能的话,我想要一个handleTap: onButton:
方法。目前,在按钮 1 到 8 上,有八种方法(handleTap1
通过)可以进行相同的操作。handleTap8
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap1:)];
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap2:)];
UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap3:)];
UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap4:)];
UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap5:)];
UITapGestureRecognizer *tap6 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap6:)];
UITapGestureRecognizer *tap7 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap7:)];
UITapGestureRecognizer *tap8 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleTap8:)];
[let1Button addGestureRecognizer: tap1];
[let2Button addGestureRecognizer: tap2];
[let3Button addGestureRecognizer: tap3];
[let4Button addGestureRecognizer: tap4];
[let5Button addGestureRecognizer: tap5];
[let6Button addGestureRecognizer: tap6];
[let7Button addGestureRecognizer: tap7];
[let8Button addGestureRecognizer: tap8];
这是其中一种方法的示例,显然我想let1Button
用传入的参数替换来表示任何按钮。
- (void) handleTap1: (UITapGestureRecognizer *) recognizer
{
[_box setText: [_box.text stringByAppendingString: [let1Button titleForState: UIControlStateNormal]]];
[let1Button setUserInteractionEnabled:NO];
[let1Button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
}