【发布时间】:2014-03-14 13:57:25
【问题描述】:
这是一个带有大按钮图像的链接。 上面带有数字 0 的红色图像是我的问题所在的文本字段。
我有一个滚动视图,它有 12 个(大)按钮,这些按钮都是以编程方式创建的。在这些按钮上,您又添加了 12 个按钮(每个大按钮上一个小按钮)并且代码运行良好。但是当我尝试添加 12 个文本字段作为子视图时,每个大按钮上的一个以编程方式我无法获得 12 个文本字段。我最终只得到一个文本字段,而且总是在特定大按钮行的最后一个按钮上。我无法确定问题所在。这是我创建大按钮、小按钮和它们上的文本字段的代码。
- (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
【问题讨论】:
-
好吧,您实际上只在 for 循环之外创建了一次 UITextField。按钮是在循环中创建的。
标签: ios objective-c uitextfield