【问题标题】:How to create group of buttons when i clicked one button (add button) in objective C?当我单击目标 C 中的一个按钮(添加按钮)时,如何创建一组按钮?
【发布时间】:2013-08-08 09:49:24
【问题描述】:

我是 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);

对不起,我的英语不好。谢谢。

【问题讨论】:

  • 但是你能解释一下你想添加 3 个按钮,或者它们可以变化吗?
  • 感谢@vikingosegundo 的回复。我想每次创建 3 个按钮。
  • @Manimaran:我没有回应。我刚刚格式化了你的问题。
  • 好的。我很抱歉。感谢@IronManGill

标签: ios objective-c cocoa-touch uibutton


【解决方案1】:

您好,请尝试以下代码:

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;
}

希望此代码对您有所帮助。

【讨论】:

    【解决方案2】:

    然后,您可以简单地在 UIButton 的方法中使用循环语句。在循环中根据 x 和 y 坐标添加按钮。每次循环执行时不断更改 x 和 y 坐标。这将防止它们相互重叠。

    【讨论】:

    • 感谢 IronManGill 的帮助。你能给我一些示例代码吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-18
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2020-03-21
    • 2015-10-16
    相关资源
    最近更新 更多