我将UIButton添加为子视图后遇到问题。当我尝试调整视图上的按钮位置时,我始终只用第一个按钮添加UIView而不是UIButton。当我使用isKindOfClass时,其余的返回为UIButton。这里是一个代码片段如下添加到子视图后,UIButton返回为UIView?
首先,我从viewDidload调用createTiles方法将按钮添加为子视图,然后通过调用adustLandscapeTiles/Portrait方法检测设备方向后调整按钮布局。
我不确定为什么会发生任何想法? ?
// Adding buttons to view
- (void)createTiles
{
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;
[self.view insertSubview:button atIndex:0];
NSLog(@"adding %i %@",i , [ [contentList objectAtIndex:i] valueForKey:@"nameKey"]);
[button release];
}
}
- (void)adustLandscapeTiles
{
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);
/// check subview is a button
NSLog(@"index: %i tag : %i Is of type UIButton?: %@",index,[[self.view viewWithTag:index] tag], ([ [self.view viewWithTag:index] isKindOfClass: [UIButton class]])? @"Yes" : @"No");
/// Only the first button added with tag zero return UView instead of UIButton ??
UIButton *tmb= (UIButton *)[self.view viewWithTag:index];
if([tmb isKindOfClass:[UIButton class]]) //isKindOfClass
{
[ [self.view viewWithTag:index] setFrame:frame];
}
}
}
}
感谢IDZ,当我试图改变在Interface Builder的观点是没有工作的标记值,但与你的偏移建议的工作完美...谢谢 – user519274 2012-07-17 14:09:53
纠正我,如果我错了,也不会该语句[self.view insertSubview:按钮atIndex:0]增加按钮的保留计数与[self.buttons insertObject:button atIndex:0]相同? 。您的方法肯定会更易于维护,而我最初将执行该方法,但我担心增加按钮的保留数量? – user519274 2012-07-17 16:56:12
保留两次只要释放两次就没有问题。因此,在您的情况下,要成为一名优秀的内存管理公民,您需要确保在'viewDidUnload'中设置'self.buttons = nil'并删除所有子视图。您应该已经在执行删除所有子视图步骤,因为您提到您正在从'viewDidLoad'调用'createTiles'。规则是你应该在'viewDidUnload'和'dealloc'中释放* viewDidLoad中创建的任何内容。 – idz 2012-07-17 19:02:25