in -init方法不会更改子视图

时间:2012-10-10 18:49:29

标签: iphone objective-c ios ios5 superclass

我有一个从UIView派生的对象,它是AIItem,这个项目有UIImageView * status_view,现在我需要另一个来自AIItem的AIAnotherItem对象,问题在于status_view。

例如:

AIItem init方法

-(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方法

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

我将另一个图像设置为status_view但它不会改变。 问题是为什么?这是怎么回事?

1 个答案:

答案 0 :(得分:0)

无论这些机制是什么都不起作用(我相信你会弄清楚),我相信你可能不会以正确的方式解决这个问题。

让类AIItem.h具有空属性statusView更合乎逻辑吗?然后是两个派生类(或同一个子类的实例)继承相同的statusView但用不同的图像填充它?

我认为这与 inheritance 背后的哲学更为接近。