如果它们都具有相同的 IBAction,如何定义按下哪个按钮?

How to define which button pressed if they both have same IBAction?(如果它们都具有相同的 IBAction,如何定义按下哪个按钮?)
本文介绍了如果它们都具有相同的 IBAction,如何定义按下哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 UIButtons(我使用 IB 创建它们),它们使用相同的 IBAction 连接到 File 的所有者,我如何定义它们中的哪一个被按下?

I have two UIButtons (I create them using IB), which connected to File's owner with the same IBAction, how can i define which of them are pressed?

推荐答案

你的动作可以这样实现:

Your action can be implemented like this:

- (IBAction) buttonTapped: (id) sender
// you can also replace id with UIButton*

然后在这个方法里面你可以通过 -isEqual: 方法检查

Then inside this method you can check by -isEqual: method

- (IBAction) buttonTapped: (id) sender
{
   if ([sender isEqual:referenceToOneOfYourButtons]) {
   // do something
   }
   else if ([sender isEqual:referenceToTheOtherButton]) {
   ...
   }
}

或者,您可以设置不同的值来标记按钮的属性,然后:

Alternatively you can set up different values to tag property of buttons and then:

- (IBAction) buttonTapped: (UIButton*) sender
{
   const int firstButtonTag = 101;
   const int otherButtonTag = 102;

   if (sender.tag == firstButtonTag) {
   ...
   }
   else if (sender.tag == otherButtonTag) {
   ...
   }
}

您需要在 .xib 或代码中设置此标记.

You need to set up this tag either in your .xib or in code.

这篇关于如果它们都具有相同的 IBAction,如何定义按下哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)