Pergunta

I Have 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]);

}

and

- (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]);

}

if i remove [enemyScrollView addSubview:button1]; then 1 UIScrollView show Buttons but when i add [enemyScrollView addSubview:button1]; then only 2 UIScrollView show Buttons ...

any ideas?

Sorry for my english

Foi útil?

Solução

This code, has a lot of problems of OOP conceptually.

First of all we begin with the problem:

You have an array of UIButton. The UIButton, can be added on a class UIView (or kind of). So, this button, can have just 1 SUPERVIEW.

In other words, if you add the button on the view1, you will see it on the view1; but if after you add this button on the view2, you will see it on the view2 and no more in the view1.

Would you be able to be in 2 place at the same time?

Other things..a better code could be this:

- (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]);

}

For the other is the same. Obviously, you need to have another set of button so you can't use the same buttons in headButtonArray..but you need another array of buttons..or change the strategy.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top