我们可以将UIView对象分配给XIB的UIView对象吗?

时间:2012-05-02 08:26:35

标签: uiview xib nib iboutlet

我通过代码创建了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]);
}

感谢

0 个答案:

没有答案