XCode:以编程方式添加多个具有特定名称和动画的按钮 [英] XCode: Add multiple buttons programmatically with specific names and animate
问题描述
我刚刚阅读了这篇关于 以编程方式添加带有循环的按钮.
I just read this post about adding buttons with a loop programmatically.
我想做同样的事情,但要为每个人指定一个名称,并以相同的方式为其制作动画.这是一个好主意,还是我应该为每个按钮复制同一行代码?
I want to do the same thing, but do it with a name for each one, and animate them the same way. Is this a good idea, or should I just copy the same line of code for each button?
此外,我想从名称列表(大于四个)中添加多个按钮(比如四个)并随机选择它们.问题是,我需要正确命名它们并提取与每个名称相关联的图像.有什么想法吗?
Further, I'd like to add a number of buttons (say four) from a list of names (greater than four) and pick them randomly. The catch is, I need them to be named properly and pull images associated with each name. Any ideas?
感谢 SO 社区!
推荐答案
如我所见,您应该使用按钮信息数组创建一个属性列表 (.plist) - 对于每个按钮都会显示文本,图片显示和操作(方法名称或其他东西).您可以在 [0, [buttonArray length]]
范围内生成四个不同的随机数,然后在循环中为每个选定的数字生成按钮.
As I see it, you should make a property list (.plist) with the array of buttons info - for each button there will be text to display, pic to display and action (method name or something).
You can generate four different random numbers in range [0, [buttonArray length]]
and then generate your buttons in the loop for each selected number.
我想,你需要类似 buttonFactory 的方法
I guess, you need something like buttonFactory with method
-(UIButton*) makeButtonWithInfo(NSDictionary*)info
名称、图片地址、动作等存储在 info 中的位置(如果它很复杂,您可以自定义类型 buttonInfo).
where name, picture adress, action etc. stored in info (you custom type buttonInfo if it is complicated).
更新:
在 Xcode (resources/PropertyList) 中创建新的 .plist 并像这样填充它:
Create new .plist in Xcode (resources/PropertyList) and fill it like this:
在您的代码中阅读它
NSArray* buttonsArray = [NSArray arrayWithContentsOfFile:myPlist.plist];
此数组中将包含带有按钮信息的字典.
There will be dictionaries with button info in this array.
阅读有关 NSArray、NSDictionary 和 UIButton 类的 Xcode 帮助并实现您的逻辑.
Read in Xcode help about NSArray, NSDictionary and UIButton classes and implement your logic.
这篇关于XCode:以编程方式添加多个具有特定名称和动画的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!