Search code examples
iphoneobjective-ciosios5superclass

in -init method won't change subviews


I have a object derived from UIView, it is AIItem, this item have UIImageView *status_view, now I need another object AIAnotherItem derived from AIItem, problem is in status_view.

For Example :

AIItem init method

-(id)initWithName:(NSString *)name {
self = [super init];
if (self) {
    status_view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,50,50)];
    status_view.image = [UIImage imageNamed:@"item_image.png"];
    [self addSubview:status_view];
}

}

AIAnotherItem init method

-(id)initWithName:(NSString *)name {
self = [super initWithName:name];
if (self) {
   status_view.image = [UIImage imageNamed:@"another_item_image.png"];
}
return self;
}

in AIAnotherItem I set another image to status_view but it won't changed. Question is why ? and how do this ?


Solution

  • Regardless what the mechanics are of this not working (I am sure you will figure it out), I believe that you are perhaps not going about this the right way.

    Would it not be more logical to have class AIItem.h that has an empty property statusView? And then two derived classes (or instances of the same subclass) that inherit the same statusView but fill it with different images?

    I think this would correspond more closely to the philosophy behind inheritance.