IOS:一个IBAction用于多个按钮
在我的项目中,我必须控制40个按钮的操作,但我不想创建40个IBAction,我可以只使用一个IBAction吗?
In my project I must control action of 40 buttons, but I don't want to create 40 IBAction, can I use only a IBAction, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用界面生成器来创建按钮,只需将它们指向相关类中的相同 IBAction 即可。
然后,您可以通过从按钮读取文本来区分 IBAction 方法中的按钮......
或者通过在 Xcode 中设置
tag
属性并通过[sender 读回它标签]
。 (如果您使用此方法,请从 1 开始标记,因为 0 是默认值,因此用处不大。)If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.
You can then differentiate between the buttons within the IBAction method either by reading the text from the button...
...or by setting the
tag
property in Xcode and reading it back via[sender tag]
. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)将所有按钮设置为使用该操作。操作通常有一个
sender
参数,您可以使用该参数来确定哪个按钮正在调用该操作。区分按钮之间差异的一种流行方法是为每个按钮的tag
属性分配不同的值。因此,您可能有 40 个按钮,其标签范围从 1 到 40。(0 可能不是标签的最佳选择,因为这是默认值,并且您忘记设置标签的任何按钮都将使用 0 作为标签 )当所有按钮都执行大致相同的操作(例如计算器或键盘上的按钮 时,此技术最有用。如果每个按钮执行完全不同的操作,那么您最终仍然会得到相当于 40 个方法的结果,但您可以用自己的 switch 语句替换 Objective-C 的消息传递系统。在这种情况下,最好花时间创建所需数量的操作并适当地分配它们。
Set all the buttons to use that one action. Actions generally have a
sender
parameter, which you can use to figure out which button is calling the action. One popular way to tell the difference between buttons is to assign a different value to each button'stag
property. So you might have 40 buttons with tags ranging from 1 to 40. (0 probably isn't a great choice for a tag since that's what the default value is, and any button for which you forget to set the tag will have 0 as the tag value.)This technique is most useful when all the buttons do approximately the same thing, like the buttons on a calculator or keyboard. If each of the buttons does something completely different, then you still end up with the equivalent of 40 methods, but you substitute your own switch statement for Objective-C's messaging system. In that case, it's often better just to spend the time to create as many actions as you need an assign them appropriately.
当然。只需将所有按钮连接到 Interface Builder 中的相同操作方法即可。使用该方法的
sender
参数(可能与按钮的tag
属性结合使用)来识别哪个按钮正在发送事件。Sure. Just connect all buttons to the same action method in Interface Builder. Use the method's
sender
argument (possibly in conjunction with the buttons'tag
property) to identify which button is sending the event.只需使用一个 IBAction 并将其分配给您的所有按钮。
Just use one IBAction and assign it to all your buttons.
我自己就使用了上述方法,选择了一些按钮,但将它们全部转换并使用 switch case 代替
Just used the above method myself , had a selection of buttons but converted them all and used switch case instead
看起来你已经得到了你需要的所有答案,但我想添加到其他人的答案中。
您想要使用 1 个 IBAction 还是 40 个操作取决于您希望按钮执行的操作。如果所有按钮执行完全不同的操作,则需要所有单独的 IBAction,但如果希望所有按钮执行相同的操作,则可以只使用一个。我需要这些按钮和操作的更多详细信息,但您可能有每个按钮的标题,因此您可以使用它来区分每个按钮并创建消息或通过按下的特定按钮自定义的内容。这是一个例子。每次按下按钮时,标签都会显示一条消息,显示“按下按钮的标题”。
通过这种方式,您不需要对所有 40 个模式进行 switch case。您仍然可以通过按下 2-3 行代码的按钮来显示或执行个性化的操作。
Seems like you are getting all the answers you need, but I wanted to add to everyone else's answers.
Whether you want to use one IBAction or 40 actions is up to what you want the buttons to do. If all the buttons do completely different things, you need all separate IBActions, but if you want all of them to do the same thing, you can use just one. I need more details of those buttons and action, but you probably have title for each button, so you can use that to differentiate each button and create a message or something that's being customized by the particular button pressed. Here is the example. Each time a button is pressed, a label displays a message that say "i.e.title of the button" pressed.
By doing it this way, you don't need to do switch case with all 40 patterns. You can still display or do something that's individualized by the button pressed with just 2-3 lines of code.