1

将 UIButton 添加为子视图后,我无法访问它。当我尝试调整视图上的按钮位置时,我总是得到一个 UIView 而不是 UIButton 只有我添加的第一个按钮。当我使用 isKindOfClass 时,其余的返回为 UIButton。这是一个代码片段如下

首先,我从 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];

            }

        }
    }

}
4

1 回答 1

1

当我为您的问题添加更强大的解决方案时,我注意到您过度释放button. 你不应该叫[button release]你不拥有它!但这不是你唯一的问题......

如果您没有为 a 分配标签,UIView它将有一个零标签,因此如果您的视图有一些您没有分配标签的其他子视图,您可能会得到其中一个。尝试为您的标签添加一些偏移量,例如:

// Somewhere early in your .m file
const NSInteger kButtonTagOffset 256

// In your createTiles
button.tag = (kButtonTagOffset + i);

显然,您在检索瓷砖时也需要添加此偏移量。

话虽如此,如果有人将您的类子类化,或者即使您稍后编辑,您当前的方法可能会被更有经验的程序员称为脆弱的方法,您可能会分配冲突的标签并导致一些问题。您可能会考虑更强大的解决方案。

既然您真正想要做的是维护为可以通过索引访问的按钮数组,那么为什么不完全不依赖标签就这样做呢?

所以在你UIViewController添加一个NSMutableArray来保存你的按钮。

// In your .h or with private methods:
@property (nonatomic, retain) NSMutableArray* buttons;

// At the top of your @implementation
@synthesize buttons;

// A modified createTiles 
// Adding buttons to view 
- (void)createTiles
{     
    self.buttons = nil; /* in case viewDidUnload id not do this. */
    self.buttons = [[[NSMutableArray alloc] init] autorelease]; 
    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];

        [self.view insertSubview:button atIndex:0];
        NSLog(@"adding %i %@", i, 
                        [[contentList objectAtIndex:i] valueForKey:@"nameKey"]);
        [self.buttons insertObject:button atIndex:0];
    }
}

- (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;    
            UIView* buttonView = [self.buttons objectAtIndex:index];

            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 Is of type UIButton?: %@", index, 
                    ([ [buttonView viewWithTag:index] isKindOfClass: [UIButton class]])?
                        @"Yes" : @"No");

            UIButton *tmb= (UIButton *)buttonView;
            if([tmb isKindOfClass:[UIButton class]]) //isKindOfClass
            {   
                [buttonView setFrame:frame];
            }
        }
    }
}
于 2012-07-17T05:31:01.710 回答