-1

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

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
pbeo
  • 399
  • 1
  • 4
  • 14

1 Answers1

1

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.

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
  • So i must create another UIButton and NSArray? – pbeo May 14 '14 at 22:58
  • another array of button yes..i don't know what you need to do..but a button is one entity..and you can't add it in 2 place at the same time. So yes, you need another array with other buttons. Please set the answer as correct so we can close the topic. – Matteo Gobbi May 14 '14 at 23:02