首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >循环,从数组中生成带有函数的按钮

循环,从数组中生成带有函数的按钮
EN

Stack Overflow用户
提问于 2012-12-04 21:05:27
回答 1查看 1.3K关注 0票数 0

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

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

到目前为止,我的代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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 ++;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 03:03:41

在这里找到了我的答案:Add a multiple buttons to a view programmatically, call the same method, determine which button it was

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13711897

复制
相关文章

相似问题

如何在带有按钮的函数中停止for循环?

13

Powershell GUI自动生成带有函数的按钮

287

从带有一个数组的循环中生成表

15

按钮数组中的循环

11

Python从数组生成函数

31
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文