Iphone 如何在循环中创建按钮并为每个按钮指定特定的标记

Iphone 如何在循环中创建按钮并为每个按钮指定特定的标记,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我想创建一组按钮。为此,我使用这样的循环 for (int i =0; i< [plistArray count];i++) { editButton = [[UIButton alloc]initWithFrame:CGRectMake(width-10, -3.6, 39, 35)]; [editButton setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal]

我想创建一组按钮。为此,我使用这样的循环

for (int i =0; i< [plistArray count];i++) {

    editButton = [[UIButton alloc]initWithFrame:CGRectMake(width-10, -3.6, 39, 35)];
    [editButton setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal];
    [editButton addTarget:self action:@selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
}
for(int i=0;i<[plistaray count];i++){
editButton=[[UIButton alloc]initWithFrame:CGRectMake(宽度-10,-3.6,39,35)];
[editButton setImage:[UIImage ImageName:@“DeleteButton.png”]用于状态:UIControlStateNormal];
[editButton addTarget:self action:@selector(deleteObjectViewImage:)for ControlEvents:UIControlEventTouchUpInside];
}
但是,当我单击任何随机按钮时,分配的函数都不会被调用。当我单击第一个按钮时,它会被调用(按顺序1、2、3)。

这里有一个:

    #define ButtonHeight 40

    #define OffsetBetweenButtons 20

    #define ButtonHeightPlusOffsetBetweenButtons (ButtonHeight+OffsetBetweenButtons)

//create 6 buttons from 0,1,2,3,4,5 so totally 6 buttons
for(int buttonIndex=0;buttonIndex<=5;buttonIndex++){

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button addTarget:self 
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchUpInside];

    [button setTitle:@"Button" forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"DeleteButton.png"] forState:UIControlStateNormal];

    //Set offset in Y axis
    button.frame = CGRectMake(20.0, ButtonHeightPlusOffsetBetweenButtons * buttonIndex , 280.0, 40.0);

    //Set Tag for future identification
    [button setTag:buttonIndex];

    [YourView addSubview:button];

}
#定义按钮权重40
#定义按钮之间的偏移量20
#定义按钮高度加上按钮之间的设置(按钮高度+按钮之间的偏移)
//从0,1,2,3,4,5创建6个按钮,总共6个按钮

对于(int buttonIndex=0;buttonIndex您想在屏幕上动态添加按钮吗?为了解释为什么我否决了这个答案:您没有解释代码或解释为什么这是您的答案,这只是一堆带有一些神奇数字的代码(以及通过宏不一致地使用常量)。换句话说,我不认为这是一个答案,因为有人只是直接为提问者编写代码。我也不喜欢你的用户名和通过按钮标题进行的自我提升-所以我也投了反对票。@nil谢谢你对我的答案感兴趣,我想回答你的问题1。为什么我在这里使用宏ver flow不仅面向专家/经验丰富的开发人员。任何人都可以访问这个问题。因此,在这里我可以简单地将60作为值,而不是那些宏。但是任何程序员都可以通过看到这些宏来理解。Objective C是一种非常描述性的语言,它将通过方法名称指定方法的用途。@nil 2。您已经指定了我觉得我是在自我推销。我不是有意的。尽管你已经明确说明了这一点,但我已经删除了。记住,你也可以编辑答案。我认为你试图回避答案整体不好的问题,所以否决票仍然存在。