0

UIButtonsプログラムで複数追加したい。実際に私は持っていNSMutableArrayます。UIButtonsそして、その数だけ多くの要素をに作成したかったのNSMutableArrayです。また、 の各行に 3 つの UIButton が必要です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
- (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 に答える