2

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!

Community
  • 1
  • 1
Kevin Brown
  • 12,602
  • 34
  • 95
  • 155

1 Answers1

4

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.

Community
  • 1
  • 1
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • And don't forget to reseed the randomness generator (`srandom(time(NULL));`), or you'll get the same sequence each time. Also, you could use an NSDictionary to hold the string names and pics or pic paths. – Wienke Feb 14 '12 at 05:33
  • This makes a little sense. I'm new to objective-c. Could you give a little more detail? – Kevin Brown Feb 14 '12 at 05:41