以编程方式创建多个标签变量

时间:2011-04-27 04:19:19

标签: ios objective-c variables label concatenation

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

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];
}

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你的代码很荒谬(而stringByAppendingString是NSString calss的一种方法,所以你不能用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;

}

答案 2 :(得分:0)

试试这个,

<强> 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

答案 3 :(得分:0)

我使用此代码在不同位置创建按钮,您可以使用UILabel而不是按钮。

     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;  
    }