我创建了2个UIButton(不同的标签)并连接到1个动作但是当同时按下时,它会以小延迟触发2个动作。
- (IBAction)keysPress:(UIButton *)sender {
UIButton *butOne = (UIButton *)[sender viewWithTag:0];
UIButton *butTwo = (UIButton *)[sender viewWithTag:1];
NSLog(@"BUT 1: %@ || BUT 2: %@",butOne, butTwo);
}
记录总是2次:
2013-02-19 09:37:40.933 TestActions[1107:c07] BUT 1: <UIButtonLabel: 0xca4d450; frame = (65 67; 9 19); text = 'â'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xca4d4c0>> || BUT 2: <UIRoundedRectButton: 0xca4d310; frame = (161 164; 139 153); opaque = NO; autoresize = RM+BM; tag = 1; layer = <CALayer: 0xca4d270>>
2013-02-19 09:37:40.935 TestActions[1107:c07] BUT 1: <UIRoundedRectButton: 0xca4c5c0; frame = (20 164; 135 153); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0xca4c6b0>> || BUT 2: (null)
如何控制这种情况? 2Buttons,1次动作 - 开火1次。
答案 0 :(得分:5)
您似乎不明白sender
是什么。发件人是触发事件的按钮,您可以根据它进行编程。
您需要保存对按钮的引用,然后在输入函数时将它们与发件人进行比较。
@property (nonatomic, weak) IBOutlet UIButton *butOne;
@property (nonatomic, weak) IBOutlet UIButton *butTwo;
-(IBAction)keysPress:(UIButton *)sender {
if(sender == [self butOne]) {
//Do button one actions
}
if(sender == [self butTwo]) {
//Do button two actions
}
}
如果您不想保留引用并使用viewWithTag
内容,我真的不会建议它,但如果您这样做,您可以:
- (IBAction)keysPress:(UIButton *)sender {
UIButton *butOne = (UIButton *)[self viewWithTag:1];
UIButton *butTwo = (UIButton *)[self viewWithTag:2];
if(sender == butOne) {
//Do button one actions
}
if(sender == butTwo) {
//Do button two actions
}
}
答案 1 :(得分:2)
- (IBAction)keysPress:(UIButton *)sender {
if(_canClick)
{
_canClick = NO;
UIButton *butOne = (UIButton *)[sender viewWithTag:0];
UIButton *butTwo = (UIButton *)[sender viewWithTag:1];
NSLog(@"BUT 1: %@ || BUT 2: %@",butOne, butTwo);
[self performSelector:@selector(makeButtonCanClick) withObject:nil afterDelay:YOURTIMEINTERVAL];
}
}
- (void)makeButtonCanClick
{
_canClick = YES;
}
_canClick
是BOOL
ivar,默认为YES