UIView子视图不显示

时间:2012-05-23 06:22:38

标签: objective-c ios uiview

我确信这是一个非常愚蠢的问题。我试图将UIImageView作为子视图添加到UIView。我正在尝试添加子视图的代码是:

for (int i = 0; i < kBoardSize; i++) {
    for (int j = 0; j < kBoardSize; j++) {

        Tile *t = [[Tile alloc] initWithPosition:CGPointMake(i, j) withSize:CGSizeMake(width, height)];

        [self insertSubview:t atIndex:0];
        //[self addSubview:t];
        //[self bringSubviewToFront:t];

        [temp addObject:t];
    }
}

相应的Tile(这是UIImageView子类)代码是:

- (id) initWithPosition:(CGPoint)position withSize:(CGSize)size
{
    self = [super initWithFrame:CGRectZero];
    [self setFrame:CGRectMake(position.x * size.width, position.y * size.width, size.width, size.height)];
    self.tag = position.x * kBoardSize + position.y;
    [self setPiece:EMPTY];
    if (self) {
        [self.layer setBorderColor:[[UIColor blackColor] CGColor]];
        [self.layer setBorderWidth:0.5];
    }
    return self;
}

现在我做的时候

NSLog(@"%@", [[self subViews] description]);

2012-05-22 23:20:51.740 XXXXXXX[8204:f803] (
    "<Tile: 0x6a79380; baseClass = UIImageView; frame = (0 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6a7ad70>>",
    "<Tile: 0x6a94180; baseClass = UIImageView; frame = (0 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x6a93e40>>",
    "<Tile: 0x6a97da0; baseClass = UIImageView; frame = (0 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 2; layer = <CALayer: 0x6a97de0>>",
    "<Tile: 0x6a7aeb0; baseClass = UIImageView; frame = (106.667 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 3; layer = <CALayer: 0x6a7aef0>>",
    "<Tile: 0x6a94610; baseClass = UIImageView; frame = (106.667 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 4; layer = <CALayer: 0x6a94650>>",
    "<Tile: 0x6a946c0; baseClass = UIImageView; frame = (106.667 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 5; layer = <CALayer: 0x6a94700>>",
    "<Tile: 0x6a94770; baseClass = UIImageView; frame = (213.333 0; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 6; layer = <CALayer: 0x6a947b0>>",
    "<Tile: 0x6a94820; baseClass = UIImageView; frame = (213.333 106.667; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 7; layer = <CALayer: 0x6a94860>>",
    "<Tile: 0x6a948d0; baseClass = UIImageView; frame = (213.333 213.333; 106.667 106.667); opaque = NO; userInteractionEnabled = NO; tag = 8; layer = <CALayer: 0x6a94910>>"
)

它显示正确添加了子视图。但是子视图实际上并没有出现在视图中。可能是什么问题?

1 个答案:

答案 0 :(得分:1)

Vashishtha Jogi,你应该看看The Designated initializer,它是如何运作的。重点是任何initSomething方法最终都会调用指定的初始化程序,因此正确调用它是很重要的。