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 :P
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, Addres开发者_Python百科s, 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.
- (IBAction)buttonPressed:(UIButton *)sender {
// remember? tag 1000 equals index 0.
NSUInteger index = sender.tag - 1000;
NSDictionary *object = [self.data objectAtIndex:index];
// do whatever you want
}
No need to subclass anything. No need to violate the model-view-controller concept.
You probably are better of using an array and tags but if you really want to subclass here is how you can do it:
Interface Builder will not let you add additional attributes.
You need to do this using a custom sub-class (written in actual code).
On Xcode select:
File-> New File-> Objective C Class (Subclass of UIView
).
Use a name like MyCustomButton.
Then you need to replace (in the .h) the :UIView
to :UIButton
(this measn you are sub-classing from UIButton and no all the way from UIView).
On the interface declaration you can set your new attributes, remember to declare them as properties so you can access them later.
You wil end up with something that looks like this:
@interface MyButton : UIButton {
NSString* name;
NSString* address;
NSNumber* age;
}
@property(nonatomic,retain)NSString* name;
@property(nonatomic,retain)NSString* address;
@property(nonatomic,retain)NSNumber* age;
@end
Remember to @synthesize
your properties.
To do the custom init you just write your own init Method:
- (id)initWith:(NSString*)aName and:(NSString*)aStreet and:(NSNumber*)anAge{
self = [super init];
self.name = aName;
self.address= aStreet;
self.age=anAge;
return sef;
}
Good luck!
精彩评论