XCode: Add multiple buttons programmatically with specific names and animate

空扰寡人 提交于 2019-12-11 03:15:54

问题


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?

Thanks SO community!


回答1:


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.

I guess, you need something like buttonFactory with method

-(UIButton*) makeButtonWithInfo(NSDictionary*)info

where name, picture adress, action etc. stored in info (you custom type buttonInfo if it is complicated).

Update:

Create new .plist in Xcode (resources/PropertyList) and fill it like this:

Read it in your code with

NSArray* buttonsArray = [NSArray arrayWithContentsOfFile:myPlist.plist];

There will be dictionaries with button info in this array.

Read in Xcode help about NSArray, NSDictionary and UIButton classes and implement your logic.



来源:https://stackoverflow.com/questions/9271890/xcode-add-multiple-buttons-programmatically-with-specific-names-and-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!