0

我想以UIButtons编程方式添加多个。其实我有NSMutableArray。我想UIButtonsNSMutableArray. 而且我还希望UIView. 创造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);
        }
4

3 回答 3

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;
        }
于 2013-04-22T09:22:46.447 回答
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;
    }
}
于 2013-04-22T09:31:40.433 回答
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];
    }
于 2013-04-22T09:24:40.603 回答