Q: I have subclassed Button and ButtonCell in my application. I am setting the cell class for my button in the -init: method for my Button subclass like this:
@implementation MyButton : Button
-init
{
[super init];
[MyButton setCellClass: [MyButtonCell class]];
return self;
}
@end
However my cell class is not being created and initialized properly. What is going on?
A: setCellClass: is a factory method and must occur prior to any instance creation--init: is too late. The correct place to do this is in the +initialize factory method for your Button subclass (which is called before any instances are created). This problem can occur with all Cell subclasses--such as TextField and NXBrowser. (+initialize is an Object factory method; see the Object class specification sheet for more information.)
@implementation MyButton : Button
+initialize
{
[super initialize];
[MyButton setCellClass: [MyButtonCell class]];
return self;
}
@end
QA761
Valid for 2.0, 3.0