Michael
Michael

Reputation: 1238

Custom initializer in subclass of UIImageView - is it necessary to override initWithImage or initWithFrame initializers?

My class is subclass of UIImageView. I create my custom initializer like this

@interface FishView : UIImageView
@end

@implementation FishView

- (id)initWithFishType:(FishType)fishType {

    self = [super init];
    if (self) {
        //my custom initialization
    }
    return self;

}

Is it necessary to override initWithImage?

- (id)initWithImage:(UIImage)image
{
    self = [super initWithImage:image];
    if (self) {
        [self initWithFishType:FTNone];
    }
    return self;
}   

Upvotes: 0

Views: 1949

Answers (3)

Dan Rosenstark
Dan Rosenstark

Reputation: 69757

In Swift 2, the only necessary initializer for UIImageView subclasses is

required init?(coder: NSCoder) { super.init(coder: coder) }

Upvotes: 0

William Entriken
William Entriken

Reputation: 39273

You only need to override if you want extra functionality in there.

But if I am reading your question as: "I need custom init functionality, shall it go in initWithImage or initWithFrame? then the answer is more complicated. Then you would actually need to override a bunch of initializers, see

Upvotes: 0

Joel
Joel

Reputation: 16124

Only if you plan to call initWithImage in your code and want it to do something special.

Upvotes: 1

Related Questions