Dynamically name labels in object c

I'm new to iOS development, just had a quick question. I am creating an application with 100 UILabel. Each label is numbered from 0 to 99. The problem is I don't want to do this for all 100 labels.

output1.text = [[NSString alloc] initWithFormat:@"1"];
output2.text = [[NSString alloc] initWithFormat:@"2"];
.....
output100.text = [[NSString alloc] initWithFormat:@"100"];

      

Instead, I would like to do it a little more dynamically. Below I am trying to use loops to create a string dynamically. For example, by adding "1.text" to the end of "output", I get the line "output1.text".

for (int i=0; i< 100; i++) {
    outputNameString = [NSMutableString stringWithCapacity:0];
    [outputNameString setString:@"output"];
    [outputNameString appendString:[NSString stringWithFormat:@"%i.text",i + 1]];
    outputNameString = [[NSString alloc] initWithFormat:@"%@",i];
}

      

"output1" to "output100" are correctly declared in the interface section and synthesized correctly. Is there something I'm missing here or is it just not possible? Any help would be appreciated.

+3


source to share


6 answers


when you create a shortcut .. set the tags from let say (100 --- 200)

So. initialize your shortcut like this.

 for (int i=0; i< 100; i++) {

UILabel *label = [[UILabel alloc] init] ;
// label formatting code here...

label.tag = i+100;
   }

      



then get your label like this .. and set its text

for (int i=0; i< 100; i++) {

UILabel *myLabel = (UILabel *)[self.view viewWithTag:i+100]; // get the label with tag
myLabel.text = [NSString stringWithFormat:@" Label %d",i+1"];
}

      

this should work fine .. what is the problem?

+9


source


If you have an array ( NSArray

) with all your labels, this is the solution:

for (int i=0; i< 100; i++) {
    UILabel *label = [arrayOfLabels objectAtIndex:i];
    label.text = [NSString stringWithFormat:@"output.%d.text", i+1];
}

      


Edit: allocating, saving and accessing 100 objects

I think it's better to have all the pointers to the labels in the array rather than tag them, especially since you have a hundred! and every time you call the method viewWithTag:

it looks for a view, it is not indexed.



Using NSArray:

@interface Object : SuperObject {
    NSArray *labels;
}
@end
@implementation
- (void)someMethodThatCreatesLabels {
    labels = [[NSMutableArray alloc] initWithCapacity:100];
    for(NSInteger i = 0; i < 100; i++){
        UILabel *label = [[UILabel alloc] ...];
        label.text = [NSString stringWithFormat:@"output.%d.text", i+1];
        [view addSubview:label]
        [labels addObject:label];
        [label release];
    }
}
- (void)methodThatAccessALabel{
    UILabel *label45 = [labels objectAtIndex:45];
    // Do your thing ...
}
- (void)dealloc{
    [labels release];
    [super dealloc];
}
@end

      

Using C arrays which is slightly shorter ( labels[i]

instead of [labels objectAtIndex:i]

)

@interface Object : SuperObject {
    UILabel **labels;
}
@end
@implementation Object
- (void)methodThatCreatesLabels
{
    labels = malloc(100*sizeOf(UILabel *));
    for(NSInteger i = 0; i < 100; i++){
        labels[i] = [[UILabel alloc] ...];
        labels[i].text = [NSString stringWithFormat:@"output.%d.text", i+1];
        [view addSubview:labels[i]]
    }
}
- (void)methodThatAccessALabel{
    UILabel *label45 = labels[45];
    // Do your thing ...
}
- (void)dealloc{
    for(int i = 0; i<100; i++) [labels[i] release];
    free(labels);
    [super dealloc];
}

      

+2


source


The way to get variables named "variables" is arrays.

You can do one of two things:

UILabel *output[100];
for(NSInteger i = 0; i < 100; i++){
  output[i] = [[UILabel alloc] ...];
}

      

This will declare an array of 100 labels from 0 to 99, and you can access them like this:

[output[50] setText:text];

      

Another way:

NSMutableArray *outputLabels = [[NSMutableArray alloc] initWithCapacity:100];
for(NSInteger i = 0; i < 100; i++){
  UILabel *label = [[UILabel alloc] ...];
  [outputLabels addObject:label];
}

      

And refer to them like this:

[[outputLabels objectAtIndex:50] setText:text];

      

In general, you should read about C arrays and then read the documentation about NSArray and NSMutableArray.

+1


source


On iOS, you can manipulate views by placing them. So for your purpose, you can create a basic view, with this you add all the labels with the appropriate tags. Later, when you need a specific shortcut, just view the request using the viewWithTag

.

This is how you can implement it:

Create shortcuts:

UIView *baseView = [[UIView alloc] init];
//keep its reference for later use.. you will need to make it instance variable if you want to access labels in other than this method.

for(int i=0;i<100;i++)
{
   UILabel *label = [[UILabel alloc] init];
   label.tag = i+1; //we are offsetting its value by 1 because tag=0 is for the baseview itself.
   label.text = <the text you wish to assign>;
   [baseView addSubview:label];
   [label release]; //if you are not using ARC.
   label = nil;
}

      

Access to shortcuts:

UILabel *label = (UILabel *)[baseView viewWithTag:<provide the tag value you wish to access>];

      

For more information on the UIView tag, you can refer to the documentation .

0


source


When you say, "Each tag is numbered from 0 to 99," do you mean they have those tags? If so, start your tags from 1 to 100, not 0 to 99. If not, set labels for each tag. (If you don't know what tags are, read about the "tag" property in the UIView documentation )

Then you can easily access these labels in a loop:

for(int i = 1; i <= 100; i++)
{
    [[self.view viewWithTag:i] setText:[NSString stringWithFormat:@"Output%d.text",i]];
}

      

If you haven't already created all the labels in the view, I would suggest creating labels programmatically, in a loop, and setting the tags. Since you can easily access them using tags, you also don't need to declare them as properties.

0


source


Maybe I'll be a little late for the show here. I'm not really sure if you want to call uilabels or set the text property to uilabels? from your example code, it looks like you are trying to set the text given that you already said you already synthesized them. if so, I can call this melody one line, bob! kvc (key value encoding) i think if your your best bet ...

for (int i=1; i< 101; i++)
{
    [self setValue:[NSString stringWithFormat:@"%i", i] forKeyPath:[NSString stringWithFormat:@"output%i.text", i]];
} 

      

the end result will look like this ...

output1.text = @"1";
// repeat...
output100.text = @"100";

      

if you want to set tags you can do the same with ...

for (int i=1; i< 101; i++)
    {
        [self setValue:i forKeyPath:[NSString stringWithFormat:@"output%i.tag", i]];
    } 

      

that the end result would be like this ...

output1.tag = 1;
// repeat...
output100.tag = 100;

      

of course, you could put both procedures in the same loop if you like. if this is what you need, i would recommend changing your code to this. this is by far the most efficient and "apple-like" way to do it. you can change the situation and tweak it how you would like to make any dynamic changes you want. this is a very cool method. he cut almost 500 lines of code in one of my projects as soon as I figured it out. it is amazing what you learn when you actually read the docs.

0


source







All Articles