我有一个简单的应用程序,我在其中以编程方式添加 UIButton,如下所示
for(int i=0;i<[self.contentList count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(buttonSelection:)forControlEvents:UIControlEventTouchDown];
[button setTitle:[ [contentList objectAtIndex:i] valueForKey:@"nameKey"] forState:UIControlStateNormal];
button.tag=i+kButtonTagOffset;
[self.view insertSubview:button atIndex:0];
// [button release];
}
然后在视图更改其方向时访问它以如下调整其位置
for (int row = 0; row < Portrait_TILE_ROWS; ++row)
{
for (int col = 0; col < Portrait_TILE_COLUMNS; ++col)
{
int index = (row * Portrait_TILE_COLUMNS) + col;
CGRect frame = CGRectMake(LandScape_TILE_MARGIN + col * (LandScape_TILE_MARGIN + LandScape_TILE_WIDTH),
LandScape_TILE_MARGIN + row * (LandScape_TILE_MARGIN + LandScape_TILE_HEIGHT),
LandScape_TILE_WIDTH, LandScape_TILE_HEIGHT);
UIButton *tmb= (UIButton *)[self.view viewWithTag:index+kButtonTagOffset];
if([tmb isKindOfClass:[UIButton class]]) //isKindOfClass
{
[ [self.view viewWithTag:index+kButtonTagOffset] setFrame:frame];
}
}
}
我的第一个问题是,如果我在添加到子视图后释放 UIBUtton 并且它只发生在设备上而不是模拟器上,为什么会出现 EXC_BAD_ACCESS 错误。我认为当添加到子视图时,对象的保留计数会增加一个并稍后访问它不会提出那个错误任何想法?
我如何在 xcode 调试器中找出什么对象以及在哪里引发了 EXC_BAD_ACCESS 异常,我通过简单地注释部分代码就知道了!
谢谢米