D
D
dimib2014-09-17 16:52:22
iOS
dimib, 2014-09-17 16:52:22

How to generalize N objects?

Good day.
I have N objects. (For example, UILabel). Their names look something like this: label1, label2, label3, ..
I need to refer to each one in turn, respectively, I write a loop

for(int i=1;i<N;i++) {
//и тут нужно как то обратиться ко всем
}

Tell me how to do it. The common name is label"i", but I don't know how to arrange it within Obj-C :(
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Morozov, 2014-09-17
@morozovdenis

create like this:

@property (nonatomic, strong) NSMutableArray *labels;

- (void)createLabels
{
    self.labels = [NSMutableArray new];
    for (int i = 0; i < N; i++)
    {
        UILabel *l = ...
        //тут вставить настройку label-а то что у вас выше
        [self.labels addObject:l];
        [self.view addSubview:l];
    }
}

iterate like this:
for(int i = 0; i < self.labels.count; i++)
{
    UILabel *l = self.labels[i];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question