我已经通过代码创建了 UIView(比如 A)对象,并通过代码添加了所有子视图,如 UIButton、UILabel 等。我在 XIB 中创建的另一个 UIView(比如 B)。现在我正在尝试将 A 分配给 B。当我在分配后记录 B 的子视图时,我可以在 B 中看到 A 的所有子视图,但视图中什么也没有出现。我知道我的逻辑可能是错误的,但我没有明白。请让我知道它是否可能!
这是我的代码:
- (UIView*)createButtonView:(NSInteger)viewTag avatarImageName:(NSString*)avatarImageName cardImageName:(NSString*)cardImageName ballImageName:(NSString*)ballImageName
{
buttonBGView = [[UIView alloc] initWithFrame:CGRectMake(15,86,290,290)];
buttonBGView.tag = viewTag;
UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 206, 84, 84)];
avatarImageView.image = [UIImage imageNamed:avatarImageName];
[buttonBGView addSubview:avatarImageView];
UIButton *temoCardButton = [[UIButton alloc] initWithFrame:CGRectMake(58, 42, 188, 222)];
temoCardButton.tag = viewTag;
[temoCardButton setImage:[UIImage imageNamed:cardImageName] forState:UIControlStateNormal];
[temoCardButton addTarget:self action:@selector(cardButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[buttonBGView addSubview:temoCardButton];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(228, 0, 30, 30)];
scoreLabel.text = [cardPointsArray objectAtIndex:randomIndex];
scoreLabel.textAlignment = UITextAlignmentCenter;
[buttonBGView addSubview:scoreLabel];
buttonBGView.backgroundColor = [UIColor redColor];
return buttonBGView;
}
-(void)cardButtonTapped:(id)sender
{
NSLog(@"%@",[buttonBGView subviews]);
self.cardTappedView = buttonBGView;
NSLog(@"%@",[self.cardTappedView subviews]);
}
谢谢