gpt4 book ai didi

ios - 如何同时实现两个UIButton?

转载 作者:行者123 更新时间:2023-12-01 17:32:42 24 4
gpt4 key购买 nike

我创建了 2 个 UIButton(不同的标签)并连接到 1 个 Action ,但是当同时按下时,它会以很小的延迟触发 2 个 Action 。

- (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)

如何控制这种情况? 2 个按钮,1 个 Action - 开火 1 次。

最佳答案

你好像不明白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
}
}

关于ios - 如何同时实现两个UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com