许多 UIButton 的共享操作,获取按钮特定的字符串
我有一个包含许多 UIButtons“汤姆和杰瑞”、“狮子王”等的视图。我希望它们全部在我的视图控制器中调用相同的操作,然后区分不同的按钮以获得按钮 -特定的 NSString(例如“TomJerry”、“LionKing”等标识符)。
想法:
- 滥用按钮的标题文本属性(自定义按钮样式、清晰的文本颜色)。
- 可以,但感觉很脏……(并且只有在按钮不需要显示文本时才有效。)
- 子类 UIButton 带有额外的 ivar + 属性“context”。
- 非常适合以编程方式添加按钮,
- 但是:我的视图是从 Nib 加载的,因此我必须为每个按钮定义一个出口(并在 awakeFromNib 或 viewDidLoad 中设置其上下文属性)。布莱赫……
- 为每个按钮创建一个具有不同“-(NSString*)context”方法实现的子类。
- 呃……认真的吗? ;-)
- 使用标签来标识按钮和用于将标签映射到特定于按钮的字符串的字典。
- DRY 违规:标签需要已知/指定两次 - 在笔尖中识别按钮,在字典中检索相应的字符串。
- 到目前为止,我一直避免使用标签。也许这是一个很好的开始机会?
如何最好地实现这一目标?有什么想法、最佳实践、实施模式吗?
更新:阅读第一个答案后(谢谢!),我相信我应该添加一些澄清点。
上述所有方法都应该有效;重点是,我并不特别喜欢其中任何一个。我问您您喜欢哪种方法(以及为什么),或者是否有其他更好的方法。
进一步的“要求”:
- 我不想将每个按钮都连接为插座。
- 我想以通用方式检索特定于按钮的字符串,即不打开操作发送者 à la
if (sender==self.button1) {...} else if (sender==self.button2) {... }
I have a view containing a number of UIButtons "Tom and Jerry", "The Lion King", etc. I want all of them to invoke the same action in my view controller, and then differentiate between the different buttons to get a button-specific NSString (e.g. identifiers like "TomJerry", "LionKing").
Ideas:
- Abuse buttons' title text property (custom button style, clear text color).
- Would work, but feels dirty… (and only works if the button doesn't need to display text.)
- Subclass UIButton with an extra ivar + property "context".
- Great for adding the buttons programmatically,
- but: my view is loaded from a Nib, so I would have to define an outlet for every button (and set its context property in awakeFromNib or viewDidLoad). Bleh…
- Create a subclass for each button with different "-(NSString*)context" method implementations.
- Ugh… seriously? ;-)
- Use tags to identify buttons and a dictionary for mapping tags to button-specific strings.
- DRY violation: The tag would need to be known/specified twice – in the Nib to identify the button, and in the dictionary to retrieve the corresponding string.
- I have thus far avoided using tags. Maybe this would be a good opportunity to start?
How best to achieve this? Any ideas, best-practices, implementation patterns?
Update: After reading the first answers (thanks!), I believe I should add some points for clarification.
All of the above approaches should work; point is, I don't particularly like any of them. I am asking you which of the approaches you would favor (and why), or whether there are other better approaches.
Further "requirements":
- I don't want to connect each button as an outlet.
- I want to retrieve the button-specific string in a generic fashion, i.e. no switch on the action sender à la
if (sender==self.button1) {…} else if (sender==self.button2) {…}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已添加
一个选项是创建一个 UIButton 类别,添加一个基于标签返回 NSString 的方法。您仍然遇到潜在的标签问题,但标签到字符串的转换(实际的和概念的)都在一个地方。
我相信这满足您最初的和进一步的要求。
以前
标签快速且轻便,可以比较两个整数。
您可以轻松创建一个编号方案,使事物清晰分开:1000-1999 是这种类型的事物,1000-1099 是这个子组,1015 是一个实例,等等。
可以进行数字比较:x.tag > 10000 人这样做。
但显然,这意味着您必须跟踪它,并且您的代码最终可能会难以解析。尽管如此,还是有一些漂亮的优势。
Added
An option would be to create a category of UIButton adding a method that returns an NSString based on the tag. You've still got the potential tag trouble but the tag-to-string conversion -- actual and conceptual -- is all in one place.
I believe this meets your original and further requirements.
Previously
Tags are fast and light, comparing two integers.
You can easily create a numbering scheme that keeps things cleanly separated: 1000-1999 is this type of thing, 1000-1099 is this subgroup, 1015 is an instance, etc.
Numeric comparisons are available: x.tag > 10000 do this.
Obviously, though, that means you have to track it and your code could end up difficult to parse down the road. Still, some nifty advantages.
您可以在按钮处理程序中比较按钮引用。
注意:我还没有编译代码。但我想你已经明白了。
我对你的想法的评论是:
更改标题文本、颜色等是非常自然的。按钮工作流程不应该太依赖于视图样式。
仅仅为了这个比较而子类化 UIButton 的工作量就太大了。
这是 2 的更肮脏的版本。
好吧,您需要非常小心,不要为两个不同的按钮使用相同的标签。
You can compare the button reference in button handler.
NOTE: I have not compiled the code. But I guess you have got the idea.
My comment about your ideas are:
It is very natural to change the title text, color etc. Button work flow should not be so much dependent on the view style.
Subclassing UIButton for just this compare is too much of work.
This is ever dirtier version of 2.
Well, you need to be very careful that you have not used same tag for two different button.
对于这种情况,最好的方法是创建 UIButton 类的子类,并添加索引等属性或任何其他对您有用的属性。
或者:在创建按钮时为其分配一些标签,根据标签您可以区分它们,并且可以执行您想要的任何操作。
For this context, the best way is to create a subclass of UIButton class, and add a property like index or any other properties that will be useful to you.
Or else: while creating buttons assign some tags to them, based on the tag you can differentiate them and you can perform whatever action that you want.