0

In my application I want to associate NSMutableDictionary to all UIElements (UIButton, UILable, etc), I can easily achieve this by subclassing each element, but I just want to know if I can make my own UIControl subclass -with a property of type NSMutableDictionary-, as superclass of all programmatically created UIElement s in anyway, so that I can reduce the number of subclasses.

Here like this

@interface UIControl : MyControls
{

}
@property(nonatomic,retain) NSMutableDictionary *details;
@end

and make MyControls as superclass of all programmatically created UIElements

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
vignesh kumar
  • 2,310
  • 2
  • 25
  • 39
  • 1
    What do you mean by associate NSMutableDictionary to all UIElements? Are you saying add an NSMutableDictionary as a property to every UIElement? So you want a UIButton to have a dictionary attached to it? What is your goal? There is probably a much better way to solve whatever problem you are dealing with. – Matt Hudson Dec 26 '12 at 15:39
  • @Inturbidus You can give that solution as answer and i'll change my question accordingly – vignesh kumar Dec 26 '12 at 15:52
  • 1
    Well that doesn't answer your question. I can probably help solve the problem, but I need to know what your goal is? – Matt Hudson Dec 26 '12 at 15:55
  • @Inturbidus I create UIElements dynamically according to an XML, so whenever user interact with those UIElements I want to update the dictionary associated with that element – vignesh kumar Dec 26 '12 at 16:02
  • Well it probably makes more sense to keep your XML in an array with IDs and set the tag of each UIButton and UILabel to the id of the XML element and update it accordingly. Subclassing is a useful technique for somethings but using the control to store data is a bad idea for many reasons including memory and accessing the data as well. – Matt Hudson Dec 26 '12 at 18:20

1 Answers1

0

You can do this, not by subclassing, but by adding your own properties and methods to UIControl itself (the superclass of UIButton, UILabel, etc.). These will then be inherited by any standard buttons, labels, etc. that you instantiate. Objective-C lets you add your own methods very easily using Categories. However, you can't add instance variables via categories. To do that, you need to use Associative References which are documented in the Objective-C Runtime Reference.

There's a good tutorial on how to do this here.

By the way, I don't necessarily disagree with inturbidus. But if you're sure you want to go this route, that's how you'd do it.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
vortek
  • 474
  • 2
  • 14
  • Categories != Subclasses. Be careful doing this. – CodaFi Dec 26 '12 at 23:04
  • @CodaFi Edited the answer to clarify that it's not subclassing. – vortek Dec 27 '12 at 00:44
  • Doesn't change my earlier point, though thank you for the clarification. Categories are still inherently... Dangerous (as in not completely safe) when it comes to associated objects. – CodaFi Dec 27 '12 at 00:47