两个UIScrollView一个不显示内容

时间:2014-05-14 22:01:52

标签: ios uiscrollview uiscrollviewdelegate

我有2个UIScrollView:

- (void)generateHeadView {

    self.headArray = [[NSArray alloc]initWithArray:self.headButtonArray];

    for (int i = 0; i < [self.headArray count]; i++) {

        UIButton *button0 = [[UIButton alloc]init];
        button0 = [self.headArray objectAtIndex:i];
        CGRect frame = CGRectMake(0, (self.playerScrollView.frame.size.height*i)+((self.playerScrollView.frame.size.height/2)-button0.frame.size.height/2), button0.frame.size.width, button0.frame.size.height);

        button0.frame = frame;

        [playerScrollView addSubview:button0];

    }

    playerScrollView.contentSize = CGSizeMake(self.playerScrollView.frame.size.width , self.playerScrollView.frame.size.height * [self.headArray count]);

}

- (void)generateEnemyHeadView {

    self.headArray = [[NSArray alloc]initWithArray:self.headButtonArray];


    for (int i = 0; i < [self.headArray count]; i++) {

        UIButton *button1 = [[UIButton alloc]init];
        button1 = [self.headArray objectAtIndex:i];
        CGRect frame = CGRectMake(0, (self.enemyScrollView.frame.size.height*i)+((self.enemyScrollView.frame.size.height/2)-button1.frame.size.height/2), button1.frame.size.width, button1.frame.size.height);

        button1.frame = frame;

        [enemyScrollView addSubview:button1];

    }
    enemyScrollView.contentSize = CGSizeMake(self.enemyScrollView.frame.size.width , self.enemyScrollView.frame.size.height * [self.headArray count]);

}

如果我删除[enemyScrollView addSubview:button1];然后1 UIScrollView显示按钮但是当我添加[enemyScrollView addSubview:button1];时,只有2个UIScrollView显示按钮......

任何想法?

抱歉我的英文

1 个答案:

答案 0 :(得分:1)

此代码存在很多 OOP概念的问题。

首先,我们从问题开始:

您有一个 UIButton数组。 UIButton可以添加到类 UIView(或类似)上。 因此,此按钮,只能 1 SUPERVIEW

换句话说,如果你在view1上添加按钮,你会在view1上看到它; 但如果在view2上添加此按钮后,您将在view2上看到它,而在view1中不会再看到它。

你能同时在两个地方吗?

其他事情..更好的代码可能是这样的:

- (void)generateHeadView {

    self.headArray = [self.headButtonArray copy]; 
    //if self.headButtonArray is mutable use copy otherwise no.

    for (int i = 0; i < [self.headArray count]; i++) {

        @autorelease {
            UIButton *button0 = [self.headArray objectAtIndex:i];

            button0.frame = CGRectMake(0, (self.playerScrollView.frame.size.height*i)+((self.playerScrollView.frame.size.height/2)-button0.frame.size.height/2), button0.frame.size.width, button0.frame.size.height);

            [playerScrollView addSubview:button0];
        }
    }

    playerScrollView.contentSize = CGSizeMake(self.playerScrollView.frame.size.width , self.playerScrollView.frame.size.height * [self.headArray count]);

}

对于另一个是相同的。显然,你需要有另一组按钮,这样你才能在headButtonArray中使用相同的按钮..但是你需要另一组按钮......或者改变策略。