UIButton在添加到子视图后返回UIView?

时间:2012-07-17 05:17:14

标签: ios uiview uibutton

在将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];

            }

        }
    }

}

1 个答案:

答案 0 :(得分:1)

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

如果您没有为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];
            }
        }
    }
}
相关问题