以编程方式创建多个textFields

时间:2014-03-14 13:57:26

标签: ios objective-c uitextfield

这是与大按钮图像的链接。 其上带有数字0的红色图像是一个textField,其中存在我的问题。

enter image description here

我有一个滚动视图,其中有12个(大)按钮以编程方式创建。在这些按钮上你添加了12个按钮(每个大按钮上有一个小按钮),代码工作正常。但是当我尝试添加12个textFields作为子视图时,每个大按钮上有一个以编程方式添加,我无法获得12个textFields。我最终得到一个textField,并且总是在特定行的大按钮的最后一个按钮上。我无法确定问题。这是我的代码,用于创建大按钮,小按钮和textFields。

 - (void)createButtons{
 NSUInteger i;
 int xCoord=54;
 int yCoord=10;
 int buttonWidth=292;
 int buttonHeight=266;
 int buttonGap = 80;
 int count=1;
 UIButton *aButton;

 //forCartButton
 int xCoordForCart=230;
 int yCoordForCart=200;
 int buttonWidthForCart=35;
 int buttonHeightForCart=36;
 int buttonGapForCart = 160;
 UIButton *aButtonForCart;

  //for textField
  int xCoordForTextField=aButton.frame.origin.x-30;
  int yCoordForTextField=aButton.frame.origin.y+210;
  int WidthForTextField=50;
  int HeightForTextField=30;
  int GapForTextField= 160;

  UITextField *textField=[[UITextField alloc]init];
  [textField setBorderStyle:UITextBorderStyleRoundedRect];
  textField.layer.cornerRadius = 15.0;
  textField.text=@"24";
  textField.backgroundColor = [UIColor redColor];
  textField.borderStyle = UITextBorderStyleRoundedRect;


    for (i = 0; i <= 11; i++)
      {
      aButton = [UIButton buttonWithType:UIButtonTypeCustom];
      aButtonForCart = [UIButton buttonWithType:UIButtonTypeCustom];

     if(count >=1 && count<=3){
         //for item button
        aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
        xCoord=xCoord+323;


        //for cart button
        aButtonForCart.frame = CGRectMake(xCoordForCart, yCoordForCart,buttonWidthForCart,buttonHeightForCart);
        xCoordForCart=xCoordForCart+2;

        //for textField
        textField.frame = CGRectMake(xCoordForTextField+50,yCoordForTextField,WidthForTextField,HeightForTextField);
        xCoordForTextField = xCoordForTextField+10;
        [aButton addSubview:textField];
        [aButton bringSubviewToFront:textField];

        if(count == 3){
            xCoord=54;
            xCoordForCart=230;
            xCoordForTextField = 230;
        }

    }else if (count >3 && count<=6){
        yCoord=buttonHeight+buttonGap;
        aButton.frame= CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
        xCoord=xCoord+323;

         //for cart button
        yCoordForCart=buttonHeightForCart+buttonGapForCart;
        aButtonForCart.frame= CGRectMake(xCoordForCart, yCoordForCart,buttonWidthForCart,buttonHeightForCart);
        xCoordForCart=xCoordForCart+2;

        //for TextField
        textField.frame = CGRectMake(xCoordForTextField+50,yCoordForTextField,WidthForTextField,HeightForTextField);
        xCoordForTextField = xCoordForTextField+10;
        [aButton addSubview:textField];
        [aButton bringSubviewToFront:textField];

        if(count == 6){
            xCoord=54;
            xCoordForCart=230;
            xCoordForTextField = 230;

        }

    }else if (count >6 && count<=9){
        yCoord=buttonHeight+buttonHeight+buttonGap+30;
        aButton.frame= CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
        xCoord=xCoord+323;

        //for cart button
        yCoordForCart=buttonHeightForCart+buttonHeightForCart+buttonGapForCart-30;
        aButtonForCart.frame= CGRectMake(xCoordForCart, yCoordForCart,buttonWidthForCart,buttonHeightForCart);
        xCoordForCart=xCoordForCart+2;

        //for textField
        textField.frame = CGRectMake(xCoordForTextField+50,yCoordForTextField,WidthForTextField,HeightForTextField);
        xCoordForTextField = xCoordForTextField+10;
        [aButton addSubview:textField];
        [aButton bringSubviewToFront:textField];

        if(count == 9){
            xCoord=54;
            xCoordForCart=230;
            xCoordForTextField = 230;

        }
    }else if (count >9 && count<=12){
        yCoord=buttonHeight+buttonHeight+buttonHeight+buttonGap+60;
        aButton.frame= CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight);
        xCoord=xCoord+323;

        //for cart button
        yCoordForCart=buttonHeightForCart+buttonHeightForCart+buttonGapForCart-30;
        aButtonForCart.frame= CGRectMake(xCoordForCart, yCoordForCart,buttonWidthForCart,buttonHeightForCart);
        xCoordForCart=xCoordForCart+2;

        //for textField
        textField.frame = CGRectMake(xCoordForTextField+50,yCoordForTextField,WidthForTextField,HeightForTextField);
        xCoordForTextField = xCoordForTextField+10;
        [aButton addSubview:textField];
        [aButton bringSubviewToFront:textField];

        if(count == 12){
            xCoord=54;
            xCoordForCart=230;
            xCoordForTextField = 230;

        }

    }
    count++;
    [aButton setTag:i];
    [aButtonForCart setTag:i];
    [aButtonForCart setImage:[UIImage imageNamed:@"cart_red.png"] forState:UIControlStateNormal];

    [aButton setImage:[UIImage imageNamed:[self.imgNames objectAtIndex:i]] forState:UIControlStateNormal];

    [aButton addSubview:aButtonForCart];
    [self.scrollViewForCatlogView addSubview:aButton];

    //selectors
[textField addTarget:self action:@selector(textFieldTouched:) forControlEvents:UIControlEventTouchDown];
    [aButton addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [aButtonForCart addTarget:self action:@selector(cartButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

}
[self.scrollViewForCatlogView setContentSize:CGSizeMake(900, yCoord+buttonHeight)];
}

我已经看过这些问题,但在我的案例中它们并没有用

1)dynamically create multiple TextFields based on array.length

2)Add UItextfield on button click

1 个答案:

答案 0 :(得分:0)

就像Christian所说,所有按钮都使用在循环外声明的相同指针。当你这样做

[self.scrollViewForCatlogView addSubview:aButton];

相同的指针被添加12次,只显示最后一个按钮。

您必须为要显示的每个视图声明一个新指针,如下所示:

for (i = 0; i <= 11; i++)
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *aButtonForCart = [UIButton buttonWithType:UIButtonTypeCustom];
}

您的UITextField可能相同。