I am looking for general guidance on how to handle this situation. Here is a specific example.
I am subclassing UIImageView and I want to override initWithImage to add my own initialization code after having the super class init itself with the supplied image.
However, there is no documented designated initializer for UIImageView, so which super class initializer should I call to ensure my subclass is correctly initialized?
If a class does not designate an initializer, do I:
- Assume that it is safe to call any of the class’s (UIImageView) initializers?
- Look to the super class (UIView) for the designated initializer?
In this case it seems #1 would be the answer as it makes sense to do the following in my overridden initializer:
- (id)initWithImage:(UIImage *)image
{
self = [super initWithImage:image];
if (self) {
// DO MY EXTRA INITIALIZATION HERE
}
return self;
}
UIImageView has two initializers, so you may want to make sure that your subclass handles both of these initialization paths.
You could simply declare that
-initWithImage:is your designated initializer, and all other initializers are not supported.Further, you could implement
-initWithImage:highlightedImage:and throw an exception to indicate that it’s not supported.Alternatively, you could declare
-initWithImage:highlightedImage:as your designated initializer, and have-initWithImage:call your designated initializer.Or, you may find that your
-initWithImage:initializer is called regardless of whether your class is initialized with-initWithImage:or-initWithImage:highlightedImage:.