多个uibutton在目标c中执行相同的功能
我正在构建一个包含许多按钮(动态生成)的应用程序。按下时所有按钮应执行相同的功能。我的问题是如何知道调用函数时按下了哪个按钮,以便执行正确的工作。
I'm building an application that contain many buttons (generated dynamically). All the buttons should execute the same function when pressed. My question is how to know which button has been pressed when the function is called, in order to perform the correct job.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
向操作处理程序发送操作消息的按钮实例将是传递给处理程序的
sender
参数。只需将其与您存储的按钮实例进行比较即可。或者,稍微有点笨拙,使用 UIView 的
tag
字段(UIButton
的超类)来区分按钮,并检查sender
的标签。The button instance that sends an action message to your action handler will be the
sender
parameter passed to the handler. Just compare this against the button instances you have stored.Alternatively, and slightly kludgy, use the
tag
field of UIView (the superclass ofUIButton
) to differentiate your buttons, and check the tag ofsender
.尝试通过正在执行的函数上的标签来识别发送者。这样您就知道按下了哪个按钮。您可以在动态生成按钮时分配标签。
您可以选择使用 NSDictionary 来保存正在生成的所有按钮标签或数组,然后相应地使用它。
Try identifying the sender by a tag on the function that is being executed. That way you will know which button was pressed. You can assign the tag the moment you generate the buttons dynamically.
Optionally you could use a NSDictionary to save all the button tags that are being generated or an Array and then use it accordingly.