我有两个 UIButton(我使用 IB 创建它们),它们使用相同的 IBAction 连接到文件的所有者,我如何定义它们中的哪一个被按下?
最佳答案
您的操作可以这样实现:
- (IBAction) buttonTapped: (id) sender
// you can also replace id with UIButton*
然后在这个方法中你可以通过 -isEqual: 方法检查
- (IBAction) buttonTapped: (id) sender
{
if ([sender isEqual:referenceToOneOfYourButtons]) {
// do something
}
else if ([sender isEqual:referenceToTheOtherButton]) {
...
}
}
或者,您可以设置不同的值来标记按钮的属性,然后:
- (IBAction) buttonTapped: (UIButton*) sender
{
const int firstButtonTag = 101;
const int otherButtonTag = 102;
if (sender.tag == firstButtonTag) {
...
}
else if (sender.tag == otherButtonTag) {
...
}
}
您需要在 .xib 或代码中设置此标记。
关于objective-c - 如果它们都具有相同的 IBAction,如何定义按下哪个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5542327/