我是iOS开发的新手。我正在创建一个包含一个按钮的应用程序,其作用类似于添加按钮。如果单击该按钮,则要创建一组三个按钮。当我单击添加按钮时,每次这些按钮也都想在上一个按钮旁边创建。我需要一个想法或示例代码。这是我的代码:
-(void) addBtnAct
{
[self.view addSubview:sizeDropDownBtn];
deleteBtn.hidden=YES;
[sizeDropDownBtn addSubview:sizeDropDownBtnLbl];
[self.view addSubview:weightDropDownBtn];
[weightDropDownBtn addSubview:weightDropDownBtnLbl];
[self.view addSubview:quantityTxtFld];
[quantityTxtFld addSubview:quantityLbl];
sizeDropDownBtn.frame = CGRectMake(10, 190, 86, 40);
weightDropDownBtn.frame = CGRectMake(110, 190, 86, 40);
quantityTxtFld.frame = CGRectMake(220, 190, 86, 40);
quantityLbl.frame = CGRectMake(10, 12, 100, 15);
sizeDropDownBtnLbl.frame = CGRectMake(20, 12, 100, 15);
weightDropDownBtnLbl.frame = CGRectMake(10,12, 100, 15);
对不起,我的英语不好。谢谢你。
您好请尝试以下代码:
int x=10;
int y=10;
for(int i=0;i<3;i++)
{
CGRect frame = CGRectMake(10, y, 280, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.tag=i;
[button setTitle:(NSString *)@"new button" forState:(UIControlState)UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"temp.png"] forState:UIControlStateNormal];
[self.view addSubview:button];
y+=45;
}
希望这段代码对您有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句