I apologize if this is a simple question but I’ve been unable to find a sufficient solution and I’m driving myself a little crazy 😛
I’ve got an app with a number of buttons (40-50) on various views, and a dictionary should be built with a number of entries based upon which button has been pressed.
(For example, if Button 27 is pressed I’d build a Dict in viewController with:
Name -> John Smith
Address -> 1 Infinite Loop
Age -> 99
)
Obviously writing an action specific to each button is wrong.
What I’d like to do is to subclass UIButton (continuing with above example), adding Name, Address, and Age as instance variables to my custom Button and to set each of this instance variables when when the button is instantiated with an init:
Then, when that button is pressed, I can have a SINGLE buildDict:(id)sender action that is called by all the buttons, and simply populate the dictionary with sender.Name etc…
My question is: can I add fields to the attributes inspector in Interface Builder to set these values? If this is not possible, where can I find the button instantiation so I can set these values programmatically?
you don’t want to put data that belongs to the model inside a view. And this sounds awfully complicated too.
Insert data in interface builder? I feel lucky if I don’t have to edit tags for my views and you want to edit data in it?
Seriously, make it right. Create a NSArray. 40 objects for 40 buttons. Use the plist editor to create the list of addresses and load that plist file into your code. This is the fastest and cleanest way to do this.
Give each button a tag. (if you have to add 40 buttons I would do it in code anyway) and connect them to a IBAction. I would suggest you to give the first item a tag of 1000. A button without a tag behaves like a button with tag 0, and you don’t want this. And you don’t want to subtract 1 if you go from button to array index.
No need to subclass anything. No need to violate the model-view-controller concept.