0

Here is a link with with an image of the big button. The red image with number 0 over it is a textField where my problem is present.

enter image description here

I have a scroll View having 12 (big)buttons that were created programmatically. On those buttons u have added 12 more buttons(one small button on each big button) and the code was working fine. But when i tried to add 12 textFields as subview, one on each big button programmatically i am unable to get twelve textFields. I end up getting just one textField and that too always on the last button of a particular row of big buttons. I am unable to identify the issue. Here is my code to create the big buttons, small buttons and the textFields on them.

 - (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)];
}

I have seen these questions but they weren't useful in my case

1) dynamically create multiple TextFields based on array.length

2) Add UItextfield on button click

1
  • 1
    Well, you actually create the UITextField only once, outside the for loop. The buttons are created within the loop. Commented Mar 14, 2014 at 14:36

1 Answer 1

0

Like Christian stated, all your buttons use the same pointer declared outside your loop. When you do

[self.scrollViewForCatlogView addSubview:aButton];

the same pointer is added 12 times, and only the last button will be displayed.

You must declare a new pointer for each view you want to display, like this :

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

It is probably the same for your UITextField.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup thanku for the help. That was the issue. I solved it by initializing the textField within the for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.