【问题标题】:creating more than one label variable programmatically以编程方式创建多个标签变量
【发布时间】:2011-04-27 04:19:19
【问题描述】:

如何以编程方式创建多个标签变量我尝试了以下代码但无法创建,有什么方法可以连接变量名和整数?

for(int intNum=0;intNum<3;intNum++)
{
    UILabel *lblText1;
    UILabel *lblmany = [lblText1 stringByAppendingString:intNum];   
    lblmany = [[UILabel alloc] initWithFrame:CGRectMake(65, 50, 200, 30)];
    lblmany.text = strLable1Caption;
    lblmany.textAlignment = UITextAlignmentCenter;  

    [self.view addSubview:lblmany];
    [lblText1 release];
    [lblmany release];
}

【问题讨论】:

  • 你想做什么?代码没有多大意义
  • 我正在尝试创建 3 个具有不同属性的标签并应放置在不同的位置
  • 我明白了,但您希望会发生什么?
  • 我需要创建 3 个具有不同属性(大小、文本等)的标签,并且应该放置在不同的位置

标签: ios objective-c variables label concatenation


【解决方案1】:

您正在创建具有相同框架的标签,它为什么会放置在不同的位置?动态定义框架,不使用静态值,如果你想应用不同的属性,如果只想使用 for 循环,请使用 switch case,否则单独定义而不是循环。

【讨论】:

    【解决方案2】:

    您的代码很荒谬(并且 stringByAppendingString 是 NSString calss 的一个 istance 方法,因此您无法使用 UILabel 访问它)。如果你想创建一些标签来做这样的事情,你想做什么。

    for(int intNum=0;intNum<3;intNum++)
    {
    
    
        UILabel *lbl;
    
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 50, 200, 30)];
    lbl.text = strLable1Caption;
    llbl.textAlignment = UITextAlignmentCenter;  
    
    
        [self.view addSubview:lbl];
    
        [lbl release];
    lbl=nil;
    
    }
    

    【讨论】:

    • 我已经尝试过了,但它会创建 3 个具有相同属性的标签,一个在另一个上,但我需要创建 3 个具有不同属性的标签并且应该放置在不同的位置
    • 你需要根据你的需要改变逻辑,我正在展示支持你制作标签的方式。您需要用 i 添加一些乘数,并为位置设置 x 和 y,并给出您想要的不同字符串
    【解决方案3】:

    试试这个,

    myController.h

          #defune MAX_LABELS 2048
            @interface myController : UIViewController
            {
                UILabel                 *myLabels[MAX_LABELS];
                NSInteger               myLabelsCount;
        }
        - (void) createMyLabels;
        - (void) removeMyLabels;
    
        @end
    

    myController.m

        @implementation myController
    
            - (void) createMyLabels
            {
                [self removeMyLabels];
    
                float x = 10.0;
                float y = 5.0;
    
    
               myLabelsCount = 0;
    
                for (int i = 0; i < [No of labels]; i++)
                {
    
                      myLabels[myLabelsCount] = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 200, 30)];
                      myLabels[myLabelsCount].text = strLable1Caption;
                      myLabels[myLabelsCount].textAlignment = UITextAlignmentCenter;  
    
    
                       [self.view addSubview:myLabels[myLabelsCount]];
                       myLabelsCount++;
                       y = y + 15.0;
                }
            }
    
            - (void) removeMyLabels
            {
                for (int i = 0; i < myLabelsCount; i++)
                {
                    [jmyLabels[i] removeFromSuperview];
                }
                myLabelsCount = 0;
            }
    
            - (void)dealloc {
    
                [super dealloc];
            }
    
    
            @end
    

    【讨论】:

    • 是的,我试过了,但它在这一行崩溃了 ------- myLabels[myLabelsCount] = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 200, 30)];
    • 我已经编辑了代码。请再试一次。对不起我的愚蠢错误。现在试试吧。
    • 实际上我已经纠正了你的错误 n 尝试但它崩溃了,即使现在它也在同一行崩溃
    • 你能检查 myLabelsCount 的值吗?在 for 循环之前将其设为 0。
    【解决方案4】:

    我使用这段代码在不同位置创建按钮,你可以使用 UILabels 代替按钮。

         int x =15;  
         int  y =12;   
         for (int i =0 ; i <numberamount; i++)   {  
            if (x>273) {   
                x=15;  
                y=y+50;  
            }  
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
            button.frame = CGRectMake(x, y, 40, 40);  
            [button setTitle:[nmb objectAtIndex:i] forState:UIControlStateNormal];  
            [button addTarget:self action:@selector(buttonPressed:)
             forControlEvents:UIControlEventTouchUpInside];  
            NSString *tagindex = [[NSString alloc]initWithFormat:@"%@",[nmb objectAtIndex:i]];  
            int tagindexint  = [tagindex intValue];  
            [button setTag:tagindexint];  
            [buttons addSubview:button];  
            [tagindex release];  
            x = x+75;  
        }  
    

    【讨论】:

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