【问题标题】:Loop, generating buttons with functions from an array循环,从数组中生成带有函数的按钮
【发布时间】:2012-12-04 21:05:26
【问题描述】:

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

我想出了如何使用 for 循环和 NSArray 在 UILabels 中生成文本列表。我不知道如何以编程方式生成 UIButtons,我也不知道如何让每个按钮从数组中复制相应的字符串(即按钮 #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 ++;
    }

【问题讨论】:

    标签: ios objective-c uibutton nsarray programmatically-created


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多