Reputation: 579
I have the following code which isn't working, can someone tell me why?
If I put label1.text = @"XX" into the loop this populates so it is obviously something to do with adding these objects into the array. Can I do this?
NSMutableArray *labels = [[NSMutableArray alloc] initWithObjects:label0.text,label1.text,label2.text,label3.text,label4.text,nil];
for(int i=0; i<labelStrings.count;i++) {
labels[i] = @"XX";
}
Upvotes: 0
Views: 2089
Reputation: 534950
When you call initWithObjects
you fetch, let's say, label0.text
. That is just a string value, the value of the label's text at this moment. It is not a magic pointer for setting the label's text or anything like that.
Then when you set labels[i]
you merely replace one string in the mutable array with a different string.
The way to do what you want to do is make an array of the labels and then, for each item of the array set its text
property.
Something like this (typed directly, not tried, so watch out, this code might have errors in it):
NSArray *labels = @[label0, label1, label2, label3, label4];
for(int i=0; i<labels.count; i++) {
[(UILabel*)(labels[i]) setText: @"XX"];
}
Upvotes: 1