0

我确信这是一个非常愚蠢的问题。我正在尝试将 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>>"
)

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

4

1 回答 1

1

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

于 2012-05-23T07:22:47.143 回答