如何以编程方式添加多个uibutton

时间:2013-04-22 09:19:46

标签: iphone objective-c uibutton

我想以编程方式添加多个UIButtons。实际上我有NSMutableArray。我希望在UIButtons中创建尽可能多的NSMutableArray个元素。我还希望UIView中每行必须有3个UIButtons。创建UIButton对于男人来说问题不是我编写用于动态创建buttons的算法的问题。这是代码,这是正确的

-(IBAction)seeAll:(id)sender
{
    if ([barBtn.title isEqualToString:@"See All"])
    {
        containerVw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
        [containerVw setBackgroundColor:[UIColor whiteColor]];
        [srcVw removeFromSuperview];
        [self.view addSubview:containerVw];

        NSUInteger i;
        int xCoord;
        int yCoord=20;
        int buttonWidth=80;
        int buttonHeight=50;
        int buffer = 10;
        for (i = 1; i <= imageArray.count; i++)
        {
            UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
             aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
            [aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal];
            [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
            [containerVw addSubview:aButton];


            if (i%4 == 0 )
            {
                yCoord += buttonHeight + buffer;
            }
            NSLog(@"xcoordinate %i",xCoord);
            NSLog(@"ycoordinate %i",yCoord);
        }

3 个答案:

答案 0 :(得分:1)

看到我的吼叫答案并从中得到一些想法..

        int imageIndex = 0;

        int yOffset = 4;

        while (imageIndex < imageArray.count)        
        {
            int yPos =  7 + yOffset * 30;

            for(int i = 0; i < 4; ++i)//set 4 for 4 columns
            {
                CGRect  rect = CGRectMake((0 + i * 80), yPos, 80, 31);

                if (imageIndex < [imageArray  count]) {

                    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame = rect;
                    button.tag = imageIndex;
                    [button addTarget:self 
                               action:@selector(btnTemp_Clicked:)
                     forControlEvents:UIControlEventTouchDown];

                    [button setTitle:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal];
  //                 [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
                    [button setBackgroundImage:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal];

                    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

                    [button setNeedsDisplay];

                    [containerVw addSubview:button];
                }
                ++imageIndex;
            }
            ++yOffset;
        }

答案 1 :(得分:1)

- (void)createButtonGrid
{
    int x, y, width, height, exceedSpace;
    exceedSpace = 6;
    x = y = exceedSpace;
    width = height = 100;

    UIButton *btn;

    for (int i=0; i< 10; i++)
    {
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(x, y, width/2, height/2)];
        [btn setTag:i];
        [btn addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];

        [self.scrollview addSubview:btn];

        if((i+1) % 3 == 0)
        {
            x = exceedSpace;
            y += (width/2)+exceedSpace;
        }
        else
            x += (width/2)+exceedSpace;
    }
}

答案 2 :(得分:0)

您在哪里设置按钮的xCoord

for (i = 1; i <= imageArray.count; i++)
    {
        if (i%4 == 0 )
        {
            yCoord += buttonHeight + buffer;
            xCoord=0;
        }
        else{
            xCoord += buttonWidht + buffer;
        }
        NSLog(@"xcoordinate %i",xCoord);
        NSLog(@"ycoordinate %i",yCoord);

        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
        [aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal];
        [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
        [containerVw addSubview:aButton];
    }