Ios 循环,使用数组中的函数生成按钮

Ios 循环,使用数组中的函数生成按钮,ios,objective-c,uibutton,nsarray,programmatically-created,Ios,Objective C,Uibutton,Nsarray,Programmatically Created,对于我的iOS应用程序,我有一个文本数组,我想在我的UIScrollview中生成右侧的文本列表和左侧的按钮列表。 每当用户从右侧按下UIButton时,左侧的文本将复制到用户设备的剪贴板 我知道了如何使用for循环和NSArray生成UILabels中的文本列表。但我不知道如何以编程方式生成UIButtons,也不知道如何让每个按钮从数组中复制相应的字符串(即button#3从数组中复制字符串#3(objectAtIndex#2) 以下是我目前的代码: NSArray *copyTex

对于我的iOS应用程序,我有一个文本数组,我想在我的UIScrollview中生成右侧的文本列表和左侧的按钮列表。 每当用户从右侧按下UIButton时,左侧的文本将复制到用户设备的剪贴板

我知道了如何使用for循环和NSArray生成UILabels中的文本列表。但我不知道如何以编程方式生成UIButtons,也不知道如何让每个按钮从数组中复制相应的字符串(即button#3从数组中复制字符串#3(objectAtIndex#2)

以下是我目前的代码:

    NSArray *copyText;
    copyText = [NSArray arrayWithObjects: @"text to copy 1", "text to copy 2", "text to copy 3", "text to copy 4", nil];

    int i = 0;
    while (i < [copyText count]) {
        UILabel *copyLabels = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (ScrollView1.frame.size.width*2/3), 25)];
        copyLabels.center = CGPointMake(ScrollView1.frame.size.width*2/3, ((i*25)+15));
        copyLabels.textAlignment = UITextAlignmentCenter;
        copyLabels.textColor = [UIColor blackColor];
        copyLabels.backgroundColor = [UIColor clearColor];
        copyLabels.font = [UIFont fontWithName:@"System" size:(16.0)];
        [ScrollView1 addSubview:copyLabels];
        copyLabels.text = [NSString stringWithString:[copyLabels objectAtIndex:i]];
        i ++;
    }
NSArray*copyText;
copyText=[NSArray arrayWithObjects:@“要复制的文本1”,“要复制的文本2”,“要复制的文本3”,“要复制的文本4”,无];
int i=0;
而(i<[copyText count]){
UILabel*copyLabels=[[UILabel alloc]initWithFrame:CGRectMake(0,0,(ScrollView1.frame.size.width*2/3),25)];
copyLabels.center=CGPointMake(ScrollView1.frame.size.width*2/3,((i*25)+15));
copyLabels.textAlignment=UITextAlignmentCenter;
copyLabels.textColor=[UIColor blackColor];
copyLabels.backgroundColor=[UIColor clearColor];
copyLabels.font=[UIFont fontWithName:@“系统”大小:(16.0)];
[滚动视图1添加子视图:复制标签];
copyLabels.text=[NSString stringWithString:[copyLabels objectAtIndex:i]];
i++;
}
在这里找到了我的答案:

UIButton有一个“tag”字段,可以在其中指定数值,这样我的循环就可以跟踪哪个按钮是哪个按钮。天才