gpt4 book ai didi

ios - UIButton 添加到 subview 后作为 UIView 返回?

转载 作者:行者123 更新时间:2023-11-29 13:32:26 24 4
gpt4 key购买 nike

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

首先,我从 viewDidload 调用 createTiles 方法将按钮添加为 subview ,然后在检测到设备方向后通过调用 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];

}

}
}

}

最佳答案

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

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

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

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

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

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

既然您真正想做的是维护您可以按索引访问的按钮数组,为什么不完全依赖标签来做到这一点呢?

因此,在您的 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];
}
}
}
}

关于ios - UIButton 添加到 subview 后作为 UIView 返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11516396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com