Добавление UIButton в подпредставление не работает - PullRequest
0 голосов
/ 01 декабря 2011

Я пытаюсь добавить 16 UIButton в коде к подпредставлению моего основного представления:

internView = [[UIView alloc] initWithFrame:CGRectMake(16, 16, (self.view.frame.size.width - 16), (self.view.frame.size.height - 16) - 60)];
internView.backgroundColor = [UIColor whiteColor];


for (int rij = 0; rij < 4; rij++) //4 rows
{
    for (int kolom = 0; kolom < 4; kolom++) //4 colomns
    {

        CGFloat x = (((width + spacing) * kolom) + spacing);
        CGFloat y = (((height + spacing) * rij) + spacing);
        int tag = ((4 * rij) + kolom);


        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor blackColor];

        [button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[projects objectAtIndex:tag]thumbnailImage]]]] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(getDetail:) forControlEvents:UIControlStateNormal];

        button.tag = tag;

        NSLog(@"%i",button.tag);

        [internView addSubview:button];

    }
}

[self.view addSubview:internView];

Подвид 'internview' виден (как я сделал это белым фоном) на главном экране, но кнопки не видны.

Также мой журнал показывает тег кнопки, который начинается с 0..15, поэтому я знаю, что они сделаны, но простые не добавляются в подпредставление 'internview'

Не могу понять, что я здесь упускаю или делаю неправильно ..

1 Ответ

7 голосов
/ 01 декабря 2011

Измените свой код на:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);

В вашей версии у вас есть:

  1. Утечка памяти из-за того, что вы создали кнопки два раза
  2. [UIButton buttonWithType: UIButtonTypeCustom]; возвращает новую кнопку с рамкой CGRectZero
...