Frage

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!

War es hilfreich?

Lösung

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: enter image description here

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top