UIButton数组每行添加3个按钮

时间:2014-12-22 14:11:54

标签: ios objective-c for-loop uibutton

我有一个带有6个按钮的UIButton数组。我想要做的是在3个块中将这些按钮添加到我的视图上 - 这意味着每行有3个按钮。

为此,我使用两个for循环。一个用于创建所有6个按钮的实例,另一个用于识别按钮计数大于3。

但是我很难添加UIButton s,一个在另一个之下。这是我的循环代码:

for(int i=1;i<6;i++)
{
    questButton=[UIButton new];
    for (int j=1; j<4; j++)
    {
        [questButton setFrame:CGRectMake(x, y, 30, 30)];
        [questButton setTitle:[NSString stringWithFormat:@"Q%d",i] forState:UIControlStateNormal];
        [questButton setTag:i];

        [self.questionBtnArray addObject:questButton];

        questButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
        [questButton release];
        x=x+40;
    }

    NSLog(@" button frame for %d button is :- %@",i,NSStringFromCGRect(questButton.frame));
    y=y+40;
}

我做错了什么?

3 个答案:

答案 0 :(得分:2)

我不是ios或xcode的人,但你的逻辑似乎不对。你说你必须将6个按钮分别放在两排3个按钮中。但你的循环产生了近6行太错误了。你不必使用嵌套循环来实现这一目标。一个如果在内就足够了。并注意在放置三个按钮后,必须重新初始化像x这样的某些值,并以y为增量移动到下一行。

试试这个:

for(int i=1;i<=6;i++)
            {
                questButton=[UIButton new];
                [questButton setFrame:CGRectMake(x, y, 30, 30)];
                [questButton setTitle:[NSString stringWithFormat:@"Q%d",i] forState:UIControlStateNormal];
                [questButton setTag:i];

                [self.questionBtnArray addObject:questButton];

                questButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
                [questButton release];
                x=x+40;

                if(i==3) // three buttons placed, move to next row at starting column
                {
                    x=0; // or whatever initial value you have
                    y=y+40;
                }

            }

答案 1 :(得分:0)

for(int i=1;i<6;i++)
            {
                for (int j=1; j<4; j++)
                {
                    UIButton *questButton=[UIButton new]
                    [questButton setFrame:CGRectMake(x, y, 30, 30)];
                    [questButton setTitle:[NSString stringWithFormat:@"Q%d",i] forState:UIControlStateNormal];
                    [questButton setTag:i];

                    [self.questionBtnArray addObject:questButton];

                    questButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
                    [questButton release];
                    x=x+40;
NSLog(@" button frame for %d button is :- %@",i,NSStringFromCGRect(questButton.frame));
                }

                y=y+40;
                x=0;
            }

答案 2 :(得分:0)

如果你想在矩阵中订购元素(这里是UIView),我会这样做:

int maxElements = 6;         // Number of elements
int maxElementsPerRow = 3;   // You want only three elements per row
int xMarge = 40;             // x space between elements
int yMarge = 40;             // y space between elements
int elementWidth = 30;       // Size of element
int elementHeight = 30; 
CGPoint topLeftPos = CGPointMake(100, 100); // Top left position of first element

int elementCounter = 0;
for (int i = 0 ; i < maxElements ; i++)
{
    int row = (int)(elementCounter / maxElementsPerRow);
    int column = (int)(elementCounter % maxElementsPerRow);
    elementCounter++;

    CGRect frame = CGRectMake((xMarge + elementWidth) * column + topLeftPos.x, 
                              (yMarge + elementHeight) * row + topLeftPos.y,
                              elementWidth,
                              elementHeight);
    UIView *v = [[UIView alloc] initWithFrame:frame]; 

    // Do what you want with your element
    v.backgroundColor = [UIColor redColor];
    [self.view addSubview:v];
}